mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
139 Commits
v5.4-alpha
...
v5.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c33b1728ae | ||
|
|
a304199836 | ||
|
|
6d7cd31fee | ||
|
|
993c58fde3 | ||
|
|
d49b288728 | ||
|
|
69e84805e4 | ||
|
|
364e569945 | ||
|
|
63295f1f7f | ||
|
|
50523b107d | ||
|
|
aa8d4a782d | ||
|
|
efcf24be0c | ||
|
|
17dbaa8639 | ||
|
|
9514abc2da | ||
|
|
0be57b9b6d | ||
|
|
61a4e64a66 | ||
|
|
9a6f47f0ed | ||
|
|
948fb628d9 | ||
|
|
a901c505ab | ||
|
|
0ddc0f47bd | ||
|
|
cac075a122 | ||
|
|
9e0a8475cd | ||
|
|
7ccc6d8290 | ||
|
|
7288528a1e | ||
|
|
513559cc47 | ||
|
|
e460752323 | ||
|
|
92594f0939 | ||
|
|
e8a52281d9 | ||
|
|
6eb53b7526 | ||
|
|
9b7987a9d1 | ||
|
|
28ef7061bb | ||
|
|
46c3587a6f | ||
|
|
69c7139ff8 | ||
|
|
5ff408d218 | ||
|
|
e3c83835e7 | ||
|
|
bd1b87c579 | ||
|
|
d7bb8df841 | ||
|
|
c646e57fd6 | ||
|
|
e0ab13c62f | ||
|
|
e0cbaa50fa | ||
|
|
3747487320 | ||
|
|
1e0ad018ce | ||
|
|
95735bda46 | ||
|
|
d30569c064 | ||
|
|
2d92102dee | ||
|
|
490ecfcaa1 | ||
|
|
e174f43807 | ||
|
|
81f2401c6d | ||
|
|
508a705c1c | ||
|
|
6f1c033d72 | ||
|
|
5f83fb6582 | ||
|
|
679dc72c08 | ||
|
|
1499680f9e | ||
|
|
bdcfae2e1c | ||
|
|
7d526e75a7 | ||
|
|
c12983cf8a | ||
|
|
ba9cd0d25a | ||
|
|
b93f3b00bb | ||
|
|
4c32d9300c | ||
|
|
6e285e5392 | ||
|
|
b8cdea0190 | ||
|
|
e592f94a64 | ||
|
|
6e1aec7a67 | ||
|
|
6055a039b5 | ||
|
|
6c0e44464b | ||
|
|
6a84c32900 | ||
|
|
6a10f03ff8 | ||
|
|
7bd1e53753 | ||
|
|
b98d41db99 | ||
|
|
b2a580bdb1 | ||
|
|
89f6a85f03 | ||
|
|
6ef366644f | ||
|
|
03cde80b58 | ||
|
|
6b2e202df5 | ||
|
|
40d8832ee0 | ||
|
|
91dad09f65 | ||
|
|
4518e5df24 | ||
|
|
72a094bda7 | ||
|
|
46b84580d6 | ||
|
|
df13f25948 | ||
|
|
643188d6e5 | ||
|
|
3df5624ff4 | ||
|
|
be78aeae4c | ||
|
|
5bc47fe830 | ||
|
|
9405472565 | ||
|
|
45948e7e55 | ||
|
|
b96b0b5abb | ||
|
|
ca13be9af7 | ||
|
|
a1d8eb2743 | ||
|
|
f64a1b175a | ||
|
|
09b4e527a0 | ||
|
|
35a28a58b3 | ||
|
|
223bb04090 | ||
|
|
fe040633a1 | ||
|
|
f645d31573 | ||
|
|
35b4efc270 | ||
|
|
0d52913804 | ||
|
|
b80077b8f3 | ||
|
|
e70f275f32 | ||
|
|
9a37dc0ce6 | ||
|
|
0eb6aa4013 | ||
|
|
7f5c31cdca | ||
|
|
9e6807c3c9 | ||
|
|
2f22c6bb79 | ||
|
|
3c0d3c6fbe | ||
|
|
440a5ee78c | ||
|
|
dc07719b0d | ||
|
|
9cdf6b7082 | ||
|
|
3c1d415bd3 | ||
|
|
d36a31e673 | ||
|
|
024a6071ca | ||
|
|
4eefef07ab | ||
|
|
9c28ed05c9 | ||
|
|
8082906c05 | ||
|
|
d6af81084d | ||
|
|
4846f7e3bb | ||
|
|
c220b0a5d0 | ||
|
|
298f383ffc | ||
|
|
758c1ef445 | ||
|
|
dd6d8db49a | ||
|
|
1fb4d53925 | ||
|
|
f6aab3ec1f | ||
|
|
be8445d7e4 | ||
|
|
3d296304ef | ||
|
|
54f7b46c1e | ||
|
|
e888976bc6 | ||
|
|
4d46289331 | ||
|
|
8eca21c2e8 | ||
|
|
924bed7297 | ||
|
|
8b7cfee26b | ||
|
|
c1a63c45f8 | ||
|
|
4487c28ced | ||
|
|
05ba288049 | ||
|
|
6b9490bd72 | ||
|
|
e4b02ca8e4 | ||
|
|
20a9853e02 | ||
|
|
1d70708a78 | ||
|
|
be73f72fcc | ||
|
|
3cd9b56ae6 | ||
|
|
d2a9b4ffb8 |
2
all
2
all
@@ -2,7 +2,7 @@ make -s -j
|
||||
cd testes/libs; make -s
|
||||
cd .. # back to directory 'testes'
|
||||
ulimit -S -s 2000
|
||||
if { ../lua all.lua; } then
|
||||
if { ../lua -W all.lua; } then
|
||||
echo -e "\n\n final OK!!!!\n\n"
|
||||
else
|
||||
echo -e "\n\n >>>> BUG!!!!\n\n"
|
||||
|
||||
97
lapi.c
97
lapi.c
@@ -170,20 +170,23 @@ LUA_API int lua_gettop (lua_State *L) {
|
||||
|
||||
|
||||
LUA_API void lua_settop (lua_State *L, int idx) {
|
||||
StkId func = L->ci->func;
|
||||
CallInfo *ci = L->ci;
|
||||
StkId func = ci->func;
|
||||
ptrdiff_t diff; /* difference for new top */
|
||||
lua_lock(L);
|
||||
if (idx >= 0) {
|
||||
StkId newtop = (func + 1) + idx;
|
||||
api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
|
||||
while (L->top < newtop)
|
||||
setnilvalue(s2v(L->top++));
|
||||
L->top = newtop;
|
||||
api_check(L, idx <= ci->top - (func + 1), "new top too large");
|
||||
diff = ((func + 1) + idx) - L->top;
|
||||
for (; diff > 0; diff--)
|
||||
setnilvalue(s2v(L->top++)); /* clear new slots */
|
||||
}
|
||||
else {
|
||||
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
||||
L->top += idx+1; /* 'subtract' index (index is negative) */
|
||||
diff = idx + 1; /* will "subtract" index (as it is negative) */
|
||||
}
|
||||
luaF_close(L, L->top, LUA_OK);
|
||||
if (diff < 0 && hastocloseCfunc(ci->nresults))
|
||||
luaF_close(L, L->top + diff, LUA_OK);
|
||||
L->top += diff; /* correct top only after closing any upvalue */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
@@ -227,7 +230,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
||||
lua_lock(L);
|
||||
fr = index2value(L, fromidx);
|
||||
to = index2value(L, toidx);
|
||||
api_check(l, isvalid(L, to), "invalid index");
|
||||
api_check(L, isvalid(L, to), "invalid index");
|
||||
setobj(L, to, fr);
|
||||
if (isupvalue(toidx)) /* function upvalue? */
|
||||
luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
|
||||
@@ -259,7 +262,7 @@ LUA_API int lua_type (lua_State *L, int idx) {
|
||||
|
||||
LUA_API const char *lua_typename (lua_State *L, int t) {
|
||||
UNUSED(L);
|
||||
api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
|
||||
api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
|
||||
return ttypename(t);
|
||||
}
|
||||
|
||||
@@ -347,23 +350,21 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
||||
|
||||
|
||||
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
|
||||
lua_Number n;
|
||||
lua_Number n = 0;
|
||||
const TValue *o = index2value(L, idx);
|
||||
int isnum = tonumber(o, &n);
|
||||
if (!isnum)
|
||||
n = 0; /* call to 'tonumber' may change 'n' even if it fails */
|
||||
if (pisnum) *pisnum = isnum;
|
||||
if (pisnum)
|
||||
*pisnum = isnum;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
|
||||
lua_Integer res;
|
||||
lua_Integer res = 0;
|
||||
const TValue *o = index2value(L, idx);
|
||||
int isnum = tointeger(o, &res);
|
||||
if (!isnum)
|
||||
res = 0; /* call to 'tointeger' may change 'n' even if it fails */
|
||||
if (pisnum) *pisnum = isnum;
|
||||
if (pisnum)
|
||||
*pisnum = isnum;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -396,10 +397,10 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
|
||||
const TValue *o = index2value(L, idx);
|
||||
switch (ttypetag(o)) {
|
||||
case LUA_TSHRSTR: return tsvalue(o)->shrlen;
|
||||
case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
|
||||
case LUA_TUSERDATA: return uvalue(o)->len;
|
||||
case LUA_TTABLE: return luaH_getn(hvalue(o));
|
||||
case LUA_VSHRSTR: return tsvalue(o)->shrlen;
|
||||
case LUA_VLNGSTR: return tsvalue(o)->u.lnglen;
|
||||
case LUA_VUSERDATA: return uvalue(o)->len;
|
||||
case LUA_VTABLE: return luaH_getn(hvalue(o));
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
@@ -445,8 +446,8 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
|
||||
LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
||||
const TValue *o = index2value(L, idx);
|
||||
switch (ttypetag(o)) {
|
||||
case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o)));
|
||||
case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA:
|
||||
case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
|
||||
case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
|
||||
return touserdata(o);
|
||||
default: {
|
||||
if (iscollectable(o))
|
||||
@@ -573,7 +574,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
|
||||
LUA_API void lua_pushboolean (lua_State *L, int b) {
|
||||
lua_lock(L);
|
||||
setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */
|
||||
if (b)
|
||||
setbtvalue(s2v(L->top));
|
||||
else
|
||||
setbfvalue(s2v(L->top));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
@@ -847,21 +851,33 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
|
||||
Table *t;
|
||||
TValue *slot;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
api_checknelems(L, n);
|
||||
t = gettable(L, idx);
|
||||
slot = luaH_set(L, t, s2v(L->top - 2));
|
||||
slot = luaH_set(L, t, key);
|
||||
setobj2t(L, slot, s2v(L->top - 1));
|
||||
invalidateTMcache(t);
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
||||
L->top -= 2;
|
||||
L->top -= n;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
aux_rawset(L, idx, s2v(L->top - 2), 2);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||
TValue k;
|
||||
setpvalue(&k, cast_voidp(p));
|
||||
aux_rawset(L, idx, &k, 1);
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
||||
Table *t;
|
||||
lua_lock(L);
|
||||
@@ -874,21 +890,6 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
||||
Table *t;
|
||||
TValue k, *slot;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = gettable(L, idx);
|
||||
setpvalue(&k, cast_voidp(p));
|
||||
slot = luaH_set(L, t, &k);
|
||||
setobj2t(L, slot, s2v(L->top - 1));
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
||||
L->top--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
||||
TValue *obj;
|
||||
Table *mt;
|
||||
@@ -1311,7 +1312,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
||||
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
||||
GCObject **owner) {
|
||||
switch (ttypetag(fi)) {
|
||||
case LUA_TCCL: { /* C closure */
|
||||
case LUA_VCCL: { /* C closure */
|
||||
CClosure *f = clCvalue(fi);
|
||||
if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
|
||||
return NULL; /* 'n' not in [1, f->nupvalues] */
|
||||
@@ -1319,7 +1320,7 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
||||
if (owner) *owner = obj2gco(f);
|
||||
return "";
|
||||
}
|
||||
case LUA_TLCL: { /* Lua closure */
|
||||
case LUA_VLCL: { /* Lua closure */
|
||||
LClosure *f = clLvalue(fi);
|
||||
TString *name;
|
||||
Proto *p = f->p;
|
||||
@@ -1382,10 +1383,10 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
|
||||
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
||||
TValue *fi = index2value(L, fidx);
|
||||
switch (ttypetag(fi)) {
|
||||
case LUA_TLCL: { /* lua closure */
|
||||
case LUA_VLCL: { /* lua closure */
|
||||
return *getupvalref(L, fidx, n, NULL);
|
||||
}
|
||||
case LUA_TCCL: { /* C closure */
|
||||
case LUA_VCCL: { /* C closure */
|
||||
CClosure *f = clCvalue(fi);
|
||||
api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
|
||||
return &f->upvalue[n - 1];
|
||||
|
||||
10
lapi.h
10
lapi.h
@@ -11,12 +11,22 @@
|
||||
#include "llimits.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/* Increments 'L->top', checking for stack overflows */
|
||||
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
|
||||
"stack overflow");}
|
||||
|
||||
|
||||
/*
|
||||
** If a call returns too many multiple returns, the callee may not have
|
||||
** stack space to accommodate all results. In this case, this macro
|
||||
** increases its stack space ('L->ci->top').
|
||||
*/
|
||||
#define adjustresults(L,nres) \
|
||||
{ if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
|
||||
|
||||
|
||||
/* Ensure the stack has at least 'n' elements */
|
||||
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
|
||||
"not enough elements in the stack")
|
||||
|
||||
|
||||
54
lauxlib.c
54
lauxlib.c
@@ -62,7 +62,7 @@ static int findfield (lua_State *L, int objidx, int level) {
|
||||
else if (findfield(L, objidx, level - 1)) { /* try recursively */
|
||||
/* stack: lib_name, lib_table, field_name (top) */
|
||||
lua_pushliteral(L, "."); /* place '.' between the two names */
|
||||
lua_replace(L, -3); /* (in the slot ocupied by table) */
|
||||
lua_replace(L, -3); /* (in the slot occupied by table) */
|
||||
lua_concat(L, 3); /* lib_name.field_name */
|
||||
return 1;
|
||||
}
|
||||
@@ -87,7 +87,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||
lua_remove(L, -2); /* remove original name */
|
||||
}
|
||||
lua_copy(L, -1, top + 1); /* copy name to proper place */
|
||||
lua_settop(L, top + 1); /* remove table "loaded" an name copy */
|
||||
lua_settop(L, top + 1); /* remove table "loaded" and name copy */
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
@@ -249,7 +249,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
if (fname)
|
||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
||||
else
|
||||
@@ -284,17 +284,17 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
|
||||
LUALIB_API int luaL_execresult (lua_State *L, int stat) {
|
||||
const char *what = "exit"; /* type of termination */
|
||||
if (stat == -1) /* error? */
|
||||
if (stat != 0 && errno != 0) /* error with an 'errno'? */
|
||||
return luaL_fileresult(L, 0, NULL);
|
||||
else {
|
||||
l_inspectstat(stat, what); /* interpret result */
|
||||
if (*what == 'e' && stat == 0) /* successful termination? */
|
||||
lua_pushboolean(L, 1);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
lua_pushstring(L, what);
|
||||
lua_pushinteger(L, stat);
|
||||
return 3; /* return true/nil,what,code */
|
||||
return 3; /* return true/fail,what,code */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,7 +476,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
|
||||
UBox *box = (UBox *)lua_touserdata(L, idx);
|
||||
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||
if (temp == NULL && newsize > 0) /* allocation error? */
|
||||
luaL_error(L, "not enough memory for buffer allocation");
|
||||
luaL_error(L, "not enough memory");
|
||||
box->box = temp;
|
||||
box->bsize = newsize;
|
||||
return temp;
|
||||
@@ -902,10 +902,10 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||
luaL_checkstack(L, nup, "too many upvalues");
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
int i;
|
||||
if (l->func == NULL) /* place holder? */
|
||||
lua_pushboolean(L, 0);
|
||||
else {
|
||||
int i;
|
||||
for (i = 0; i < nup; i++) /* copy upvalues to the top */
|
||||
lua_pushvalue(L, -nup);
|
||||
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
|
||||
@@ -995,36 +995,52 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
|
||||
|
||||
static int panic (lua_State *L) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "error object is not a string";
|
||||
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
lua_tostring(L, -1));
|
||||
msg);
|
||||
return 0; /* return to Lua to abort */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Emit a warning. '*previoustocont' signals whether previous message
|
||||
** was to be continued by the current one.
|
||||
** Emit a warning. '*warnstate' means:
|
||||
** 0 - warning system is off;
|
||||
** 1 - ready to start a new message;
|
||||
** 2 - previous message is to be continued.
|
||||
*/
|
||||
static void warnf (void *ud, const char *message, int tocont) {
|
||||
int *previoustocont = (int *)ud;
|
||||
if (!*previoustocont) /* previous message was the last? */
|
||||
int *warnstate = (int *)ud;
|
||||
if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */
|
||||
if (strcmp(message, "@off") == 0)
|
||||
*warnstate = 0;
|
||||
else if (strcmp(message, "@on") == 0)
|
||||
*warnstate = 1;
|
||||
return;
|
||||
}
|
||||
else if (*warnstate == 0) /* warnings off? */
|
||||
return;
|
||||
if (*warnstate == 1) /* previous message was the last? */
|
||||
lua_writestringerror("%s", "Lua warning: "); /* start a new warning */
|
||||
lua_writestringerror("%s", message); /* write message */
|
||||
if (!tocont) /* is this the last part? */
|
||||
if (tocont) /* not the last part? */
|
||||
*warnstate = 2; /* to be continued */
|
||||
else { /* last part */
|
||||
lua_writestringerror("%s", "\n"); /* finish message with end-of-line */
|
||||
*previoustocont = tocont;
|
||||
*warnstate = 1; /* ready to start a new message */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_State *luaL_newstate (void) {
|
||||
lua_State *L = lua_newstate(l_alloc, NULL);
|
||||
if (L) {
|
||||
int *previoustocont; /* space for warning state */
|
||||
int *warnstate; /* space for warning state */
|
||||
lua_atpanic(L, &panic);
|
||||
previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0);
|
||||
warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0);
|
||||
luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */
|
||||
*previoustocont = 0; /* next message starts a new warning */
|
||||
lua_setwarnf(L, warnf, previoustocont);
|
||||
*warnstate = 0; /* default is warnings off */
|
||||
lua_setwarnf(L, warnf, warnstate);
|
||||
}
|
||||
return L;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
|
||||
/* global table */
|
||||
#define LUA_GNAME "_G"
|
||||
#define LUA_GNAME "_G"
|
||||
|
||||
|
||||
typedef struct luaL_Buffer luaL_Buffer;
|
||||
@@ -153,6 +153,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
||||
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
|
||||
|
||||
|
||||
/* push the value used to represent failure/error */
|
||||
#define luaL_pushfail(L) lua_pushnil(L)
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Generic Buffer manipulation
|
||||
@@ -181,6 +185,8 @@ struct luaL_Buffer {
|
||||
|
||||
#define luaL_addsize(B,s) ((B)->n += (s))
|
||||
|
||||
#define luaL_buffsub(B,s) ((B)->n -= (s))
|
||||
|
||||
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
|
||||
LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
|
||||
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
|
||||
|
||||
10
lbaselib.c
10
lbaselib.c
@@ -48,9 +48,9 @@ static int luaB_warn (lua_State *L) {
|
||||
luaL_checkstring(L, 1); /* at least one argument */
|
||||
for (i = 2; i <= n; i++)
|
||||
luaL_checkstring(L, i); /* make sure all arguments are strings */
|
||||
for (i = 1; i <= n; i++) /* compose warning */
|
||||
for (i = 1; i < n; i++) /* compose warning */
|
||||
lua_warning(L, lua_tostring(L, i), 1);
|
||||
lua_warning(L, "", 0); /* close warning */
|
||||
lua_warning(L, lua_tostring(L, n), 0); /* close warning */
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ static int luaB_tonumber (lua_State *L) {
|
||||
return 1;
|
||||
} /* else not a number */
|
||||
} /* else not a number */
|
||||
lua_pushnil(L); /* not a number */
|
||||
luaL_pushfail(L); /* not a number */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -308,9 +308,9 @@ static int load_aux (lua_State *L, int status, int envidx) {
|
||||
return 1;
|
||||
}
|
||||
else { /* error (message is on top of the stack) */
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
lua_insert(L, -2); /* put before error message */
|
||||
return 2; /* return nil plus error message */
|
||||
return 2; /* return fail plus error message */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
402
lcode.c
402
lcode.c
@@ -52,7 +52,7 @@ l_noret luaK_semerror (LexState *ls, const char *msg) {
|
||||
** If expression is a numeric constant, fills 'v' with its value
|
||||
** and returns 1. Otherwise, returns 0.
|
||||
*/
|
||||
static int tonumeral(const expdesc *e, TValue *v) {
|
||||
static int tonumeral (const expdesc *e, TValue *v) {
|
||||
if (hasjumps(e))
|
||||
return 0; /* not a numeral */
|
||||
switch (e->k) {
|
||||
@@ -67,6 +67,45 @@ static int tonumeral(const expdesc *e, TValue *v) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get the constant value from a constant expression
|
||||
*/
|
||||
static TValue *const2val (FuncState *fs, const expdesc *e) {
|
||||
lua_assert(e->k == VCONST);
|
||||
return &fs->ls->dyd->actvar.arr[e->u.info].k;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If expression is a constant, fills 'v' with its value
|
||||
** and returns 1. Otherwise, returns 0.
|
||||
*/
|
||||
int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
|
||||
if (hasjumps(e))
|
||||
return 0; /* not a constant */
|
||||
switch (e->k) {
|
||||
case VFALSE:
|
||||
setbfvalue(v);
|
||||
return 1;
|
||||
case VTRUE:
|
||||
setbtvalue(v);
|
||||
return 1;
|
||||
case VNIL:
|
||||
setnilvalue(v);
|
||||
return 1;
|
||||
case VKSTR: {
|
||||
setsvalue(fs->ls->L, v, e->u.strval);
|
||||
return 1;
|
||||
}
|
||||
case VCONST: {
|
||||
setobj(fs->ls->L, v, const2val(fs, e));
|
||||
return 1;
|
||||
}
|
||||
default: return tonumeral(e, v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the previous instruction of the current code. If there
|
||||
** may be a jump target between the current instruction and the
|
||||
@@ -74,7 +113,7 @@ static int tonumeral(const expdesc *e, TValue *v) {
|
||||
** optimizations).
|
||||
*/
|
||||
static Instruction *previousinstruction (FuncState *fs) {
|
||||
static const Instruction invalidinstruction = -1;
|
||||
static const Instruction invalidinstruction = ~(Instruction)0;
|
||||
if (fs->pc > fs->lasttarget)
|
||||
return &fs->f->code[fs->pc - 1]; /* previous instruction */
|
||||
else
|
||||
@@ -323,12 +362,12 @@ static void removelastlineinfo (FuncState *fs) {
|
||||
Proto *f = fs->f;
|
||||
int pc = fs->pc - 1; /* last instruction coded */
|
||||
if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */
|
||||
fs->previousline -= f->lineinfo[pc]; /* last line saved */
|
||||
fs->iwthabs--;
|
||||
fs->previousline -= f->lineinfo[pc]; /* correct last line saved */
|
||||
fs->iwthabs--; /* undo previous increment */
|
||||
}
|
||||
else { /* absolute line information */
|
||||
lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
|
||||
fs->nabslineinfo--; /* remove it */
|
||||
lua_assert(f->abslineinfo[fs->nabslineinfo].pc = pc);
|
||||
fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */
|
||||
}
|
||||
}
|
||||
@@ -348,7 +387,7 @@ static void removelastinstruction (FuncState *fs) {
|
||||
** Emit instruction 'i', checking for array sizes and saving also its
|
||||
** line information. Return 'i' position.
|
||||
*/
|
||||
static int luaK_code (FuncState *fs, Instruction i) {
|
||||
int luaK_code (FuncState *fs, Instruction i) {
|
||||
Proto *f = fs->f;
|
||||
/* put new instruction in code array */
|
||||
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
||||
@@ -458,7 +497,7 @@ void luaK_reserveregs (FuncState *fs, int n) {
|
||||
)
|
||||
*/
|
||||
static void freereg (FuncState *fs, int reg) {
|
||||
if (reg >= fs->nactvar) {
|
||||
if (reg >= luaY_nvarstack(fs)) {
|
||||
fs->freereg--;
|
||||
lua_assert(reg == fs->freereg);
|
||||
}
|
||||
@@ -537,7 +576,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
|
||||
/*
|
||||
** Add a string to list of constants and return its index.
|
||||
*/
|
||||
int luaK_stringK (FuncState *fs, TString *s) {
|
||||
static int stringK (FuncState *fs, TString *s) {
|
||||
TValue o;
|
||||
setsvalue(fs->ls->L, &o, s);
|
||||
return addk(fs, &o, &o); /* use string itself as key */
|
||||
@@ -568,11 +607,21 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
|
||||
|
||||
|
||||
/*
|
||||
** Add a boolean to list of constants and return its index.
|
||||
** Add a false to list of constants and return its index.
|
||||
*/
|
||||
static int boolK (FuncState *fs, int b) {
|
||||
static int boolF (FuncState *fs) {
|
||||
TValue o;
|
||||
setbvalue(&o, b);
|
||||
setbfvalue(&o);
|
||||
return addk(fs, &o, &o); /* use boolean itself as key */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Add a true to list of constants and return its index.
|
||||
*/
|
||||
static int boolT (FuncState *fs) {
|
||||
TValue o;
|
||||
setbtvalue(&o);
|
||||
return addk(fs, &o, &o); /* use boolean itself as key */
|
||||
}
|
||||
|
||||
@@ -590,12 +639,12 @@ static int nilK (FuncState *fs) {
|
||||
|
||||
|
||||
/*
|
||||
** Check whether 'i' can be stored in an 'sC' operand.
|
||||
** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C)
|
||||
** but without risk of overflows in the addition.
|
||||
** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
|
||||
** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
|
||||
** overflows in the hidden addition inside 'int2sC'.
|
||||
*/
|
||||
static int fitsC (lua_Integer i) {
|
||||
return (-OFFSET_sC <= i && i <= MAXARG_C - OFFSET_sC);
|
||||
return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
|
||||
}
|
||||
|
||||
|
||||
@@ -615,35 +664,67 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
||||
}
|
||||
|
||||
|
||||
static int floatI (lua_Number f, lua_Integer *fi) {
|
||||
return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi));
|
||||
}
|
||||
|
||||
|
||||
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
|
||||
lua_Integer fi;
|
||||
if (floatI(f, &fi))
|
||||
if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
|
||||
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
|
||||
else
|
||||
luaK_codek(fs, reg, luaK_numberK(fs, f));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert a constant in 'v' into an expression description 'e'
|
||||
*/
|
||||
static void const2exp (TValue *v, expdesc *e) {
|
||||
switch (ttypetag(v)) {
|
||||
case LUA_VNUMINT:
|
||||
e->k = VKINT; e->u.ival = ivalue(v);
|
||||
break;
|
||||
case LUA_VNUMFLT:
|
||||
e->k = VKFLT; e->u.nval = fltvalue(v);
|
||||
break;
|
||||
case LUA_VFALSE:
|
||||
e->k = VFALSE;
|
||||
break;
|
||||
case LUA_VTRUE:
|
||||
e->k = VTRUE;
|
||||
break;
|
||||
case LUA_VNIL:
|
||||
e->k = VNIL;
|
||||
break;
|
||||
case LUA_VSHRSTR: case LUA_VLNGSTR:
|
||||
e->k = VKSTR; e->u.strval = tsvalue(v);
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Fix an expression to return the number of results 'nresults'.
|
||||
** Either 'e' is a multi-ret expression (function call or vararg)
|
||||
** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
|
||||
** 'e' must be a multi-ret expression (function call or vararg).
|
||||
*/
|
||||
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
||||
Instruction *pc = &getinstruction(fs, e);
|
||||
if (e->k == VCALL) /* expression is an open function call? */
|
||||
SETARG_C(*pc, nresults + 1);
|
||||
else if (e->k == VVARARG) {
|
||||
else {
|
||||
lua_assert(e->k == VVARARG);
|
||||
SETARG_C(*pc, nresults + 1);
|
||||
SETARG_A(*pc, fs->freereg);
|
||||
luaK_reserveregs(fs, 1);
|
||||
}
|
||||
else lua_assert(nresults == LUA_MULTRET);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert a VKSTR to a VK
|
||||
*/
|
||||
static void str2K (FuncState *fs, expdesc *e) {
|
||||
lua_assert(e->k == VKSTR);
|
||||
e->u.info = stringK(fs, e->u.strval);
|
||||
e->k = VK;
|
||||
}
|
||||
|
||||
|
||||
@@ -672,18 +753,22 @@ void luaK_setoneret (FuncState *fs, expdesc *e) {
|
||||
|
||||
|
||||
/*
|
||||
** Ensure that expression 'e' is not a variable.
|
||||
** Ensure that expression 'e' is not a variable (nor a constant).
|
||||
** (Expression still may have jump lists.)
|
||||
*/
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
switch (e->k) {
|
||||
case VCONST: {
|
||||
const2exp(const2val(fs, e), e);
|
||||
break;
|
||||
}
|
||||
case VLOCAL: { /* already in a register */
|
||||
e->u.info = e->u.var.idx;
|
||||
e->u.info = e->u.var.sidx;
|
||||
e->k = VNONRELOC; /* becomes a non-relocatable value */
|
||||
break;
|
||||
}
|
||||
case VUPVAL: { /* move value to some (pending) register */
|
||||
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0);
|
||||
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
|
||||
e->k = VRELOC;
|
||||
break;
|
||||
}
|
||||
@@ -731,10 +816,17 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
luaK_nil(fs, reg, 1);
|
||||
break;
|
||||
}
|
||||
case VFALSE: case VTRUE: {
|
||||
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
||||
case VFALSE: {
|
||||
luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
|
||||
break;
|
||||
}
|
||||
case VTRUE: {
|
||||
luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
|
||||
break;
|
||||
}
|
||||
case VKSTR: {
|
||||
str2K(fs, e);
|
||||
} /* FALLTHROUGH */
|
||||
case VK: {
|
||||
luaK_codek(fs, reg, e->u.info);
|
||||
break;
|
||||
@@ -779,9 +871,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
||||
}
|
||||
|
||||
|
||||
static int code_loadbool (FuncState *fs, int A, int b, int jump) {
|
||||
static int code_loadbool (FuncState *fs, int A, OpCode op) {
|
||||
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
||||
return luaK_codeABC(fs, op, A, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -815,8 +907,8 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
||||
int p_t = NO_JUMP; /* position of an eventual LOAD true */
|
||||
if (need_value(fs, e->t) || need_value(fs, e->f)) {
|
||||
int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
|
||||
p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */
|
||||
p_t = code_loadbool(fs, reg, 1, 0); /* load true */
|
||||
p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */
|
||||
p_t = code_loadbool(fs, reg, OP_LOADTRUE);
|
||||
/* jump around these booleans if 'e' is not a test */
|
||||
luaK_patchtohere(fs, fj);
|
||||
}
|
||||
@@ -850,7 +942,7 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
||||
if (e->k == VNONRELOC) { /* expression already has a register? */
|
||||
if (!hasjumps(e)) /* no jumps? */
|
||||
return e->u.info; /* result is already in a register */
|
||||
if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
|
||||
if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
|
||||
exp2reg(fs, e, e->u.info); /* put final result in it */
|
||||
return e->u.info;
|
||||
}
|
||||
@@ -890,11 +982,12 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
|
||||
if (!hasjumps(e)) {
|
||||
int info;
|
||||
switch (e->k) { /* move constants to 'k' */
|
||||
case VTRUE: info = boolK(fs, 1); break;
|
||||
case VFALSE: info = boolK(fs, 0); break;
|
||||
case VTRUE: info = boolT(fs); break;
|
||||
case VFALSE: info = boolF(fs); break;
|
||||
case VNIL: info = nilK(fs); break;
|
||||
case VKINT: info = luaK_intK(fs, e->u.ival); break;
|
||||
case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
|
||||
case VKSTR: info = stringK(fs, e->u.strval); break;
|
||||
case VK: info = e->u.info; break;
|
||||
default: return 0; /* not a constant */
|
||||
}
|
||||
@@ -939,12 +1032,12 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
switch (var->k) {
|
||||
case VLOCAL: {
|
||||
freeexp(fs, ex);
|
||||
exp2reg(fs, ex, var->u.var.idx); /* compute 'ex' into proper place */
|
||||
exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */
|
||||
return;
|
||||
}
|
||||
case VUPVAL: {
|
||||
int e = luaK_exp2anyreg(fs, ex);
|
||||
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.var.idx, 0);
|
||||
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
|
||||
break;
|
||||
}
|
||||
case VINDEXUP: {
|
||||
@@ -1029,7 +1122,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
||||
pc = e->u.info; /* save jump position */
|
||||
break;
|
||||
}
|
||||
case VK: case VKFLT: case VKINT: case VTRUE: {
|
||||
case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
|
||||
pc = NO_JUMP; /* always true; do nothing */
|
||||
break;
|
||||
}
|
||||
@@ -1074,13 +1167,12 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
||||
** Code 'not e', doing constant folding.
|
||||
*/
|
||||
static void codenot (FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: case VFALSE: {
|
||||
e->k = VTRUE; /* true == not nil == not false */
|
||||
break;
|
||||
}
|
||||
case VK: case VKFLT: case VKINT: case VTRUE: {
|
||||
case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
|
||||
e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
|
||||
break;
|
||||
}
|
||||
@@ -1143,15 +1235,16 @@ static int isSCint (expdesc *e) {
|
||||
** Check whether expression 'e' is a literal integer or float in
|
||||
** proper range to fit in a register (sB or sC).
|
||||
*/
|
||||
static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) {
|
||||
static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
|
||||
lua_Integer i;
|
||||
if (e->k == VKINT)
|
||||
*i = e->u.ival;
|
||||
else if (!(e->k == VKFLT && floatI(e->u.nval, i)))
|
||||
return 0; /* not a number */
|
||||
else
|
||||
i = e->u.ival;
|
||||
else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
|
||||
*isfloat = 1;
|
||||
if (!hasjumps(e) && fitsC(*i)) {
|
||||
*i += OFFSET_sC;
|
||||
else
|
||||
return 0; /* not a number */
|
||||
if (!hasjumps(e) && fitsC(i)) {
|
||||
*pi = int2sC(cast_int(i));
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
@@ -1166,18 +1259,20 @@ static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) {
|
||||
** values in registers.
|
||||
*/
|
||||
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
||||
if (k->k == VKSTR)
|
||||
str2K(fs, k);
|
||||
lua_assert(!hasjumps(t) &&
|
||||
(t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
|
||||
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */
|
||||
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
|
||||
luaK_exp2anyreg(fs, t); /* put it in a register */
|
||||
if (t->k == VUPVAL) {
|
||||
t->u.ind.t = t->u.var.idx; /* upvalue index */
|
||||
t->u.ind.t = t->u.info; /* upvalue index */
|
||||
t->u.ind.idx = k->u.info; /* literal string */
|
||||
t->k = VINDEXUP;
|
||||
}
|
||||
else {
|
||||
/* register index of the table */
|
||||
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.idx: t->u.info;
|
||||
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info;
|
||||
if (isKstr(fs, k)) {
|
||||
t->u.ind.idx = k->u.info; /* literal string */
|
||||
t->k = VINDEXSTR;
|
||||
@@ -1218,7 +1313,7 @@ static int validop (int op, TValue *v1, TValue *v2) {
|
||||
** (In this case, 'e1' has the final result.)
|
||||
*/
|
||||
static int constfolding (FuncState *fs, int op, expdesc *e1,
|
||||
const expdesc *e2) {
|
||||
const expdesc *e2) {
|
||||
TValue v1, v2, res;
|
||||
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
||||
return 0; /* non-numeric operands or not safe to fold */
|
||||
@@ -1257,18 +1352,18 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
|
||||
** (everything but logical operators 'and'/'or' and comparison
|
||||
** operators).
|
||||
** Expression to produce final result will be encoded in 'e1'.
|
||||
** Because 'luaK_exp2anyreg' can free registers, its calls must be
|
||||
** in "stack order" (that is, first on 'e2', which may have more
|
||||
** recent registers to be released).
|
||||
*/
|
||||
static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
|
||||
OpCode op, int v2, int k, int line) {
|
||||
OpCode op, int v2, int flip, int line,
|
||||
OpCode mmop, TMS event) {
|
||||
int v1 = luaK_exp2anyreg(fs, e1);
|
||||
int pc = luaK_codeABCk(fs, op, 0, v1, v2, k);
|
||||
int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
|
||||
freeexps(fs, e1, e2);
|
||||
e1->u.info = pc;
|
||||
e1->k = VRELOC; /* all those operations are relocatable */
|
||||
luaK_fixline(fs, line);
|
||||
luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */
|
||||
luaK_fixline(fs, line);
|
||||
}
|
||||
|
||||
|
||||
@@ -1279,17 +1374,43 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
|
||||
static void codebinexpval (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
|
||||
finishbinexpval(fs, e1, e2, op, v2, 0, line);
|
||||
lua_assert(OP_ADD <= op && op <= OP_SHR);
|
||||
finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN,
|
||||
cast(TMS, (op - OP_ADD) + TM_ADD));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Code binary operators ('+', '-', ...) with immediate operands.
|
||||
** Code binary operators with immediate operands.
|
||||
*/
|
||||
static void codebini (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int k, int line) {
|
||||
int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */
|
||||
finishbinexpval(fs, e1, e2, op, v2, k, line);
|
||||
expdesc *e1, expdesc *e2, int flip, int line,
|
||||
TMS event) {
|
||||
int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */
|
||||
lua_assert(e2->k == VKINT);
|
||||
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
|
||||
}
|
||||
|
||||
|
||||
/* Try to code a binary operator negating its second operand.
|
||||
** For the metamethod, 2nd operand must keep its original value.
|
||||
*/
|
||||
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
|
||||
OpCode op, int line, TMS event) {
|
||||
if (!luaK_isKint(e2))
|
||||
return 0; /* not an integer constant */
|
||||
else {
|
||||
lua_Integer i2 = e2->u.ival;
|
||||
if (!(fitsC(i2) && fitsC(-i2)))
|
||||
return 0; /* not in the proper range */
|
||||
else { /* operating a small integer constant */
|
||||
int v2 = cast_int(i2);
|
||||
finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
|
||||
/* correct metamethod argument */
|
||||
SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
|
||||
return 1; /* successfully coded */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1300,19 +1421,18 @@ static void swapexps (expdesc *e1, expdesc *e2) {
|
||||
|
||||
/*
|
||||
** Code arithmetic operators ('+', '-', ...). If second operand is a
|
||||
** constant in the proper range, use variant opcodes with immediate
|
||||
** operands or K operands.
|
||||
** constant in the proper range, use variant opcodes with K operands.
|
||||
*/
|
||||
static void codearith (FuncState *fs, OpCode op,
|
||||
static void codearith (FuncState *fs, BinOpr opr,
|
||||
expdesc *e1, expdesc *e2, int flip, int line) {
|
||||
if (isSCint(e2)) /* immediate operand? */
|
||||
codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line);
|
||||
else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
|
||||
TMS event = cast(TMS, opr + TM_ADD);
|
||||
if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
|
||||
int v2 = e2->u.info; /* K index */
|
||||
op = cast(OpCode, op - OP_ADD + OP_ADDK);
|
||||
finishbinexpval(fs, e1, e2, op, v2, flip, line);
|
||||
OpCode op = cast(OpCode, opr + OP_ADDK);
|
||||
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
|
||||
}
|
||||
else { /* 'e2' is neither an immediate nor a K operand */
|
||||
OpCode op = cast(OpCode, opr + OP_ADD);
|
||||
if (flip)
|
||||
swapexps(e1, e2); /* back to original order */
|
||||
codebinexpval(fs, op, e1, e2, line); /* use standard operators */
|
||||
@@ -1325,14 +1445,17 @@ static void codearith (FuncState *fs, OpCode op,
|
||||
** numeric constant, change order of operands to try to use an
|
||||
** immediate or K operator.
|
||||
*/
|
||||
static void codecommutative (FuncState *fs, OpCode op,
|
||||
static void codecommutative (FuncState *fs, BinOpr op,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
int flip = 0;
|
||||
if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
|
||||
swapexps(e1, e2); /* change order */
|
||||
flip = 1;
|
||||
}
|
||||
codearith(fs, op, e1, e2, flip, line);
|
||||
if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
|
||||
codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD);
|
||||
else
|
||||
codearith(fs, op, e1, e2, flip, line);
|
||||
}
|
||||
|
||||
|
||||
@@ -1342,41 +1465,23 @@ static void codecommutative (FuncState *fs, OpCode op,
|
||||
*/
|
||||
static void codebitwise (FuncState *fs, BinOpr opr,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
int inv = 0;
|
||||
int flip = 0;
|
||||
int v2;
|
||||
OpCode op;
|
||||
if (e1->k == VKINT && luaK_exp2RK(fs, e1)) {
|
||||
swapexps(e1, e2); /* 'e2' will be the constant operand */
|
||||
inv = 1;
|
||||
flip = 1;
|
||||
}
|
||||
else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */
|
||||
op = cast(OpCode, opr - OPR_BAND + OP_BAND);
|
||||
op = cast(OpCode, opr + OP_ADD);
|
||||
codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */
|
||||
return;
|
||||
}
|
||||
v2 = e2->u.info; /* index in K array */
|
||||
op = cast(OpCode, opr - OPR_BAND + OP_BANDK);
|
||||
op = cast(OpCode, opr + OP_ADDK);
|
||||
lua_assert(ttisinteger(&fs->f->k[v2]));
|
||||
finishbinexpval(fs, e1, e2, op, v2, inv, line);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Code shift operators. If second operand is constant, use immediate
|
||||
** operand (negating it if shift is in the other direction).
|
||||
*/
|
||||
static void codeshift (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
if (isSCint(e2)) {
|
||||
int changedir = 0;
|
||||
if (op == OP_SHL) {
|
||||
changedir = 1;
|
||||
e2->u.ival = -(e2->u.ival);
|
||||
}
|
||||
codebini(fs, OP_SHRI, e1, e2, changedir, line);
|
||||
}
|
||||
else
|
||||
codebinexpval(fs, op, e1, e2, line);
|
||||
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK,
|
||||
cast(TMS, opr + TM_ADD));
|
||||
}
|
||||
|
||||
|
||||
@@ -1386,18 +1491,18 @@ static void codeshift (FuncState *fs, OpCode op,
|
||||
*/
|
||||
static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
|
||||
int r1, r2;
|
||||
lua_Integer im;
|
||||
int im;
|
||||
int isfloat = 0;
|
||||
if (isSCnumber(e2, &im, &isfloat)) {
|
||||
/* use immediate operand */
|
||||
r1 = luaK_exp2anyreg(fs, e1);
|
||||
r2 = cast_int(im);
|
||||
r2 = im;
|
||||
op = cast(OpCode, (op - OP_LT) + OP_LTI);
|
||||
}
|
||||
else if (isSCnumber(e1, &im, &isfloat)) {
|
||||
/* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
|
||||
r1 = luaK_exp2anyreg(fs, e2);
|
||||
r2 = cast_int(im);
|
||||
r2 = im;
|
||||
op = (op == OP_LT) ? OP_GTI : OP_GEI;
|
||||
}
|
||||
else { /* regular case, compare two registers */
|
||||
@@ -1416,17 +1521,17 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
|
||||
*/
|
||||
static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
||||
int r1, r2;
|
||||
lua_Integer im;
|
||||
int im;
|
||||
int isfloat = 0; /* not needed here, but kept for symmetry */
|
||||
OpCode op;
|
||||
if (e1->k != VNONRELOC) {
|
||||
lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
|
||||
swapexps(e1, e2);
|
||||
}
|
||||
r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */
|
||||
r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */
|
||||
if (isSCnumber(e2, &im, &isfloat)) {
|
||||
op = OP_EQI;
|
||||
r2 = cast_int(im); /* immediate operand */
|
||||
r2 = im; /* immediate operand */
|
||||
}
|
||||
else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */
|
||||
op = OP_EQK;
|
||||
@@ -1447,11 +1552,12 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
||||
*/
|
||||
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
||||
static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (op) {
|
||||
case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
|
||||
if (constfolding(fs, op + LUA_OPUNM, e, &ef))
|
||||
break;
|
||||
/* FALLTHROUGH */
|
||||
/* else */ /* FALLTHROUGH */
|
||||
case OPR_LEN:
|
||||
codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
|
||||
break;
|
||||
@@ -1466,6 +1572,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
||||
** 2nd operand.
|
||||
*/
|
||||
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
luaK_dischargevars(fs, v);
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
|
||||
@@ -1497,8 +1604,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
}
|
||||
case OPR_LT: case OPR_LE:
|
||||
case OPR_GT: case OPR_GE: {
|
||||
lua_Integer dummy;
|
||||
int dummy2;
|
||||
int dummy, dummy2;
|
||||
if (!isSCnumber(v, &dummy, &dummy2))
|
||||
luaK_exp2anyreg(fs, v);
|
||||
/* else keep numeral, which may be an immediate operand */
|
||||
@@ -1535,17 +1641,18 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
|
||||
*/
|
||||
void luaK_posfix (FuncState *fs, BinOpr opr,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
luaK_dischargevars(fs, e2);
|
||||
if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
|
||||
return; /* done by folding */
|
||||
switch (opr) {
|
||||
case OPR_AND: {
|
||||
lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */
|
||||
luaK_dischargevars(fs, e2);
|
||||
lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */
|
||||
luaK_concat(fs, &e2->f, e1->f);
|
||||
*e1 = *e2;
|
||||
break;
|
||||
}
|
||||
case OPR_OR: {
|
||||
lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */
|
||||
luaK_dischargevars(fs, e2);
|
||||
lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */
|
||||
luaK_concat(fs, &e2->t, e1->t);
|
||||
*e1 = *e2;
|
||||
break;
|
||||
@@ -1556,35 +1663,39 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
|
||||
break;
|
||||
}
|
||||
case OPR_ADD: case OPR_MUL: {
|
||||
if (!constfolding(fs, opr + LUA_OPADD, e1, e2))
|
||||
codecommutative(fs, cast(OpCode, opr + OP_ADD), e1, e2, line);
|
||||
codecommutative(fs, opr, e1, e2, line);
|
||||
break;
|
||||
}
|
||||
case OPR_SUB: case OPR_DIV:
|
||||
case OPR_IDIV: case OPR_MOD: case OPR_POW: {
|
||||
if (!constfolding(fs, opr + LUA_OPADD, e1, e2))
|
||||
codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line);
|
||||
case OPR_SUB: {
|
||||
if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
|
||||
break; /* coded as (r1 + -I) */
|
||||
/* ELSE */
|
||||
} /* FALLTHROUGH */
|
||||
case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
|
||||
codearith(fs, opr, e1, e2, 0, line);
|
||||
break;
|
||||
}
|
||||
case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
|
||||
if (!constfolding(fs, opr + LUA_OPADD, e1, e2))
|
||||
codebitwise(fs, opr, e1, e2, line);
|
||||
codebitwise(fs, opr, e1, e2, line);
|
||||
break;
|
||||
}
|
||||
case OPR_SHL: {
|
||||
if (!constfolding(fs, LUA_OPSHL, e1, e2)) {
|
||||
if (isSCint(e1)) {
|
||||
swapexps(e1, e2);
|
||||
codebini(fs, OP_SHLI, e1, e2, 1, line);
|
||||
}
|
||||
else
|
||||
codeshift(fs, OP_SHL, e1, e2, line);
|
||||
if (isSCint(e1)) {
|
||||
swapexps(e1, e2);
|
||||
codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */
|
||||
}
|
||||
else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
|
||||
/* coded as (r1 >> -I) */;
|
||||
}
|
||||
else /* regular case (two registers) */
|
||||
codebinexpval(fs, OP_SHL, e1, e2, line);
|
||||
break;
|
||||
}
|
||||
case OPR_SHR: {
|
||||
if (!constfolding(fs, LUA_OPSHR, e1, e2))
|
||||
codeshift(fs, OP_SHR, e1, e2, line);
|
||||
if (isSCint(e2))
|
||||
codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
|
||||
else /* regular case (two registers) */
|
||||
codebinexpval(fs, OP_SHR, e1, e2, line);
|
||||
break;
|
||||
}
|
||||
case OPR_EQ: case OPR_NE: {
|
||||
@@ -1618,6 +1729,17 @@ void luaK_fixline (FuncState *fs, int line) {
|
||||
}
|
||||
|
||||
|
||||
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
|
||||
Instruction *inst = &fs->f->code[pc];
|
||||
int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */
|
||||
int extra = asize / (MAXARG_C + 1); /* higher bits of array size */
|
||||
int rc = asize % (MAXARG_C + 1); /* lower bits of array size */
|
||||
int k = (extra > 0); /* true iff needs extra argument */
|
||||
*inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
|
||||
*(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Emit a SETLIST instruction.
|
||||
** 'base' is register that keeps table;
|
||||
@@ -1626,17 +1748,17 @@ void luaK_fixline (FuncState *fs, int line) {
|
||||
** table (or LUA_MULTRET to add up to stack top).
|
||||
*/
|
||||
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
|
||||
int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
|
||||
int b = (tostore == LUA_MULTRET) ? 0 : tostore;
|
||||
lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
|
||||
if (c <= MAXARG_C)
|
||||
luaK_codeABC(fs, OP_SETLIST, base, b, c);
|
||||
else if (c <= MAXARG_Ax) {
|
||||
luaK_codeABC(fs, OP_SETLIST, base, b, 0);
|
||||
codeextraarg(fs, c);
|
||||
if (tostore == LUA_MULTRET)
|
||||
tostore = 0;
|
||||
if (nelems <= MAXARG_C)
|
||||
luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
|
||||
else {
|
||||
int extra = nelems / (MAXARG_C + 1);
|
||||
nelems %= (MAXARG_C + 1);
|
||||
luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
|
||||
codeextraarg(fs, extra);
|
||||
}
|
||||
else
|
||||
luaX_syntaxerror(fs->ls, "constructor too long");
|
||||
fs->freereg = base + 1; /* free registers with list values */
|
||||
}
|
||||
|
||||
@@ -1675,10 +1797,10 @@ void luaK_finish (FuncState *fs) {
|
||||
SET_OPCODE(*pc, OP_RETURN);
|
||||
} /* FALLTHROUGH */
|
||||
case OP_RETURN: case OP_TAILCALL: {
|
||||
if (fs->needclose || p->is_vararg) {
|
||||
SETARG_C(*pc, p->is_vararg ? p->numparams + 1 : 0);
|
||||
SETARG_k(*pc, 1); /* signal that there is extra work */
|
||||
}
|
||||
if (fs->needclose)
|
||||
SETARG_k(*pc, 1); /* signal that it needs to close */
|
||||
if (p->is_vararg)
|
||||
SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
|
||||
break;
|
||||
}
|
||||
case OP_JMP: {
|
||||
|
||||
17
lcode.h
17
lcode.h
@@ -24,19 +24,27 @@
|
||||
** grep "ORDER OPR" if you change these enums (ORDER OP)
|
||||
*/
|
||||
typedef enum BinOpr {
|
||||
/* arithmetic operators */
|
||||
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
|
||||
OPR_DIV,
|
||||
OPR_IDIV,
|
||||
OPR_DIV, OPR_IDIV,
|
||||
/* bitwise operators */
|
||||
OPR_BAND, OPR_BOR, OPR_BXOR,
|
||||
OPR_SHL, OPR_SHR,
|
||||
/* string operator */
|
||||
OPR_CONCAT,
|
||||
/* comparison operators */
|
||||
OPR_EQ, OPR_LT, OPR_LE,
|
||||
OPR_NE, OPR_GT, OPR_GE,
|
||||
/* logical operators */
|
||||
OPR_AND, OPR_OR,
|
||||
OPR_NOBINOPR
|
||||
} BinOpr;
|
||||
|
||||
|
||||
/* true if operation is foldable (that is, it is arithmetic or bitwise) */
|
||||
#define foldbinop(op) ((op) <= OPR_SHR)
|
||||
|
||||
|
||||
#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
|
||||
|
||||
|
||||
@@ -51,16 +59,17 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
||||
|
||||
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
|
||||
|
||||
LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
|
||||
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
|
||||
LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx);
|
||||
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
|
||||
int B, int C, int k);
|
||||
LUAI_FUNC int luaK_isKint (expdesc *e);
|
||||
LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
|
||||
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
|
||||
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
||||
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
||||
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
||||
LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
|
||||
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
|
||||
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
||||
@@ -85,6 +94,8 @@ LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
|
||||
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
||||
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
|
||||
expdesc *v2, int line);
|
||||
LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc,
|
||||
int ra, int asize, int hsize);
|
||||
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
|
||||
LUAI_FUNC void luaK_finish (FuncState *fs);
|
||||
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg);
|
||||
|
||||
@@ -116,7 +116,8 @@ static int luaB_yield (lua_State *L) {
|
||||
#define COS_NORM 3
|
||||
|
||||
|
||||
static const char *statname[] = {"running", "dead", "suspended", "normal"};
|
||||
static const char *const statname[] =
|
||||
{"running", "dead", "suspended", "normal"};
|
||||
|
||||
|
||||
static int auxstatus (lua_State *L, lua_State *co) {
|
||||
|
||||
41
lctype.c
41
lctype.c
@@ -16,6 +16,15 @@
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
#if defined (LUA_UCID) /* accept UniCode IDentifiers? */
|
||||
/* consider all non-ascii codepoints to be alphabetic */
|
||||
#define NONA 0x01
|
||||
#else
|
||||
#define NONA 0x00 /* default */
|
||||
#endif
|
||||
|
||||
|
||||
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
||||
0x00, /* EOZ */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
||||
@@ -34,22 +43,22 @@ LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
||||
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */
|
||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
||||
NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
#endif /* } */
|
||||
|
||||
50
ldblib.c
50
ldblib.c
@@ -21,10 +21,10 @@
|
||||
|
||||
|
||||
/*
|
||||
** The hook table at registry[&HOOKKEY] maps threads to their current
|
||||
** hook function. (We only need the unique address of 'HOOKKEY'.)
|
||||
** The hook table at registry[HOOKKEY] maps threads to their current
|
||||
** hook function.
|
||||
*/
|
||||
static const int HOOKKEY = 0;
|
||||
static const char *const HOOKKEY = "_HOOKKEY";
|
||||
|
||||
|
||||
/*
|
||||
@@ -65,7 +65,7 @@ static int db_setmetatable (lua_State *L) {
|
||||
static int db_getuservalue (lua_State *L) {
|
||||
int n = (int)luaL_optinteger(L, 2, 1);
|
||||
if (lua_type(L, 1) != LUA_TUSERDATA)
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
|
||||
lua_pushboolean(L, 1);
|
||||
return 2;
|
||||
@@ -80,7 +80,7 @@ static int db_setuservalue (lua_State *L) {
|
||||
luaL_checkany(L, 2);
|
||||
lua_settop(L, 2);
|
||||
if (!lua_setiuservalue(L, 1, n))
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ static int db_getinfo (lua_State *L) {
|
||||
}
|
||||
else { /* stack level */
|
||||
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
|
||||
lua_pushnil(L); /* level out of range */
|
||||
luaL_pushfail(L); /* level out of range */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -202,8 +202,6 @@ static int db_getinfo (lua_State *L) {
|
||||
static int db_getlocal (lua_State *L) {
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
const char *name;
|
||||
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
|
||||
if (lua_isfunction(L, arg + 1)) { /* function argument? */
|
||||
lua_pushvalue(L, arg + 1); /* push function */
|
||||
@@ -211,6 +209,8 @@ static int db_getlocal (lua_State *L) {
|
||||
return 1; /* return only name (there is no value) */
|
||||
}
|
||||
else { /* stack-level argument */
|
||||
lua_Debug ar;
|
||||
const char *name;
|
||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
||||
return luaL_argerror(L, arg+1, "level out of range");
|
||||
@@ -223,7 +223,7 @@ static int db_getlocal (lua_State *L) {
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L); /* no name (nor value) */
|
||||
luaL_pushfail(L); /* no name (nor value) */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -314,7 +314,7 @@ static int db_upvaluejoin (lua_State *L) {
|
||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||
static const char *const hooknames[] =
|
||||
{"call", "return", "line", "count", "tail call"};
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
|
||||
lua_pushthread(L);
|
||||
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
||||
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
||||
@@ -367,14 +367,12 @@ static int db_sethook (lua_State *L) {
|
||||
count = (int)luaL_optinteger(L, arg + 3, 0);
|
||||
func = hookf; mask = makemask(smask, count);
|
||||
}
|
||||
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 */
|
||||
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
|
||||
/* table just created; initialize it */
|
||||
lua_pushstring(L, "k");
|
||||
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
||||
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
|
||||
}
|
||||
checkstack(L, L1, 1);
|
||||
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
||||
@@ -391,12 +389,14 @@ static int db_gethook (lua_State *L) {
|
||||
char buff[5];
|
||||
int mask = lua_gethookmask(L1);
|
||||
lua_Hook hook = lua_gethook(L1);
|
||||
if (hook == NULL) /* no hook? */
|
||||
lua_pushnil(L);
|
||||
if (hook == NULL) { /* no hook? */
|
||||
luaL_pushfail(L);
|
||||
return 1;
|
||||
}
|
||||
else if (hook != hookf) /* external hook? */
|
||||
lua_pushliteral(L, "external hook");
|
||||
else { /* hook table must exist */
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
|
||||
checkstack(L, L1, 1);
|
||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
||||
@@ -417,7 +417,7 @@ static int db_debug (lua_State *L) {
|
||||
return 0;
|
||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||
lua_pcall(L, 0, 0, 0))
|
||||
lua_writestringerror("%s\n", lua_tostring(L, -1));
|
||||
lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
|
||||
lua_settop(L, 0); /* remove eventual returns */
|
||||
}
|
||||
}
|
||||
@@ -437,6 +437,17 @@ static int db_traceback (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int db_setcstacklimit (lua_State *L) {
|
||||
int limit = (int)luaL_checkinteger(L, 1);
|
||||
int res = lua_setcstacklimit(L, limit);
|
||||
if (res == 0)
|
||||
lua_pushboolean(L, 0);
|
||||
else
|
||||
lua_pushinteger(L, res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static const luaL_Reg dblib[] = {
|
||||
{"debug", db_debug},
|
||||
{"getuservalue", db_getuservalue},
|
||||
@@ -454,6 +465,7 @@ static const luaL_Reg dblib[] = {
|
||||
{"setmetatable", db_setmetatable},
|
||||
{"setupvalue", db_setupvalue},
|
||||
{"traceback", db_traceback},
|
||||
{"setcstacklimit", db_setcstacklimit},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
58
ldebug.c
58
ldebug.c
@@ -31,7 +31,7 @@
|
||||
|
||||
|
||||
|
||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
|
||||
#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL)
|
||||
|
||||
|
||||
/* Active Lua function (given call info) */
|
||||
@@ -101,19 +101,21 @@ int luaG_getfuncline (const Proto *f, int pc) {
|
||||
}
|
||||
|
||||
|
||||
static int currentline (CallInfo *ci) {
|
||||
static int getcurrentline (CallInfo *ci) {
|
||||
return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** This function can be called asynchronously (e.g. during a signal),
|
||||
** under "reasonable" assumptions. A new 'ci' is completely linked
|
||||
** in the list before it becomes part of the "active" list, and
|
||||
** we assume that pointers are atomic (see comment in next function).
|
||||
** (If we traverse one more item, there is no problem. If we traverse
|
||||
** one less item, the worst that can happen is that the signal will
|
||||
** not interrupt the script.)
|
||||
** Set 'trap' for all active Lua frames.
|
||||
** This function can be called during a signal, under "reasonable"
|
||||
** assumptions. A new 'ci' is completely linked in the list before it
|
||||
** becomes part of the "active" list, and we assume that pointers are
|
||||
** atomic; see comment in next function.
|
||||
** (A compiler doing interprocedural optimizations could, theoretically,
|
||||
** reorder memory writes in such a way that the list could be
|
||||
** temporarily broken while inserting a new element. We simply assume it
|
||||
** has no good reasons to do that.)
|
||||
*/
|
||||
static void settraps (CallInfo *ci) {
|
||||
for (; ci != NULL; ci = ci->previous)
|
||||
@@ -123,8 +125,8 @@ static void settraps (CallInfo *ci) {
|
||||
|
||||
|
||||
/*
|
||||
** This function can be called asynchronously (e.g. during a signal),
|
||||
** under "reasonable" assumptions.
|
||||
** This function can be called during a signal, under "reasonable"
|
||||
** assumptions.
|
||||
** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
|
||||
** 'resethookcount') are for debug only, and it is no problem if they
|
||||
** get arbitrary values (causes at most one wrong hook call). 'hookmask'
|
||||
@@ -306,7 +308,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
|
||||
Table *t = luaH_new(L); /* new table to store active lines */
|
||||
sethvalue2s(L, L->top, t); /* push it on stack */
|
||||
api_incr_top(L);
|
||||
setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
|
||||
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
|
||||
for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */
|
||||
currentline = nextline(p, currentline, i);
|
||||
luaH_setint(L, t, currentline, &v); /* table[line] = true */
|
||||
@@ -339,7 +341,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
|
||||
ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1;
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
@@ -465,12 +467,14 @@ static int filterpc (int pc, int jmptarget) {
|
||||
|
||||
|
||||
/*
|
||||
** try to find last instruction before 'lastpc' that modified register 'reg'
|
||||
** Try to find last instruction before 'lastpc' that modified register 'reg'.
|
||||
*/
|
||||
static int findsetreg (const Proto *p, int lastpc, int reg) {
|
||||
int pc;
|
||||
int setreg = -1; /* keep last instruction that changed 'reg' */
|
||||
int jmptarget = 0; /* any code before this address is conditional */
|
||||
if (testMMMode(GET_OPCODE(p->code[lastpc])))
|
||||
lastpc--; /* previous instruction was not actually executed */
|
||||
for (pc = 0; pc < lastpc; pc++) {
|
||||
Instruction i = p->code[pc];
|
||||
OpCode op = GET_OPCODE(i);
|
||||
@@ -620,24 +624,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
|
||||
case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
|
||||
tm = TM_NEWINDEX;
|
||||
break;
|
||||
case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI:
|
||||
case OP_POWI: case OP_DIVI: case OP_IDIVI: {
|
||||
int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */
|
||||
tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
|
||||
break;
|
||||
}
|
||||
case OP_ADDK: case OP_SUBK: case OP_MULK: case OP_MODK:
|
||||
case OP_POWK: case OP_DIVK: case OP_IDIVK:
|
||||
case OP_BANDK: case OP_BORK: case OP_BXORK: {
|
||||
int offset = GET_OPCODE(i) - OP_ADDK; /* ORDER OP */
|
||||
tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
|
||||
break;
|
||||
}
|
||||
case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD:
|
||||
case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND:
|
||||
case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: {
|
||||
int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */
|
||||
tm = cast(TMS, offset + TM_ADD); /* ORDER TM */
|
||||
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
|
||||
tm = cast(TMS, GETARG_C(i));
|
||||
break;
|
||||
}
|
||||
case OP_UNM: tm = TM_UNM; break;
|
||||
@@ -648,8 +636,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
|
||||
case OP_LT: case OP_LE: case OP_LTI: case OP_LEI:
|
||||
*name = "order"; /* '<=' can call '__lt', etc. */
|
||||
return "metamethod";
|
||||
case OP_SHRI: case OP_SHLI:
|
||||
*name = "shift";
|
||||
case OP_CLOSE: case OP_RETURN:
|
||||
*name = "close";
|
||||
return "metamethod";
|
||||
default:
|
||||
return NULL; /* cannot find a reasonable name */
|
||||
@@ -789,7 +777,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
|
||||
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
|
||||
va_end(argp);
|
||||
if (isLua(ci)) /* if Lua function, add source:line information */
|
||||
luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
|
||||
luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
|
||||
luaG_errormsg(L);
|
||||
}
|
||||
|
||||
|
||||
58
ldo.c
58
ldo.c
@@ -139,9 +139,9 @@ l_noret luaD_throw (lua_State *L, int errcode) {
|
||||
|
||||
|
||||
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
l_uint32 oldnCcalls = L->nCcalls - L->nci;
|
||||
global_State *g = G(L);
|
||||
l_uint32 oldnCcalls = g->Cstacklimit - (L->nCcalls + L->nci);
|
||||
struct lua_longjmp lj;
|
||||
lua_assert(L->nCcalls >= L->nci);
|
||||
lj.status = LUA_OK;
|
||||
lj.previous = L->errorJmp; /* chain new error handler */
|
||||
L->errorJmp = &lj;
|
||||
@@ -149,7 +149,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
||||
(*f)(L, ud);
|
||||
);
|
||||
L->errorJmp = lj.previous; /* restore old error handler */
|
||||
L->nCcalls = oldnCcalls + L->nci;
|
||||
L->nCcalls = g->Cstacklimit - oldnCcalls - L->nci;
|
||||
return lj.status;
|
||||
}
|
||||
|
||||
@@ -348,18 +348,18 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) {
|
||||
|
||||
|
||||
/*
|
||||
** Check whether __call metafield of 'func' is a function. If so, put
|
||||
** it in stack below original 'func' so that 'luaD_call' can call
|
||||
** it. Raise an error if __call metafield is not a function.
|
||||
** Check whether 'func' has a '__call' metafield. If so, put it in the
|
||||
** stack, below original 'func', so that 'luaD_call' can call it. Raise
|
||||
** an error if there is no '__call' metafield.
|
||||
*/
|
||||
void luaD_tryfuncTM (lua_State *L, StkId func) {
|
||||
const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL);
|
||||
StkId p;
|
||||
if (unlikely(!ttisfunction(tm)))
|
||||
luaG_typeerror(L, s2v(func), "call");
|
||||
for (p = L->top; p > func; p--)
|
||||
if (unlikely(ttisnil(tm)))
|
||||
luaG_typeerror(L, s2v(func), "call"); /* nothing to call */
|
||||
for (p = L->top; p > func; p--) /* open space for metamethod */
|
||||
setobjs2s(L, p, p-1);
|
||||
L->top++; /* assume EXTRA_STACK */
|
||||
L->top++; /* stack space pre-allocated by the caller */
|
||||
setobj2s(L, func, tm); /* metamethod is the new function to be called */
|
||||
}
|
||||
|
||||
@@ -422,7 +422,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
|
||||
|
||||
|
||||
|
||||
#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
|
||||
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
|
||||
|
||||
|
||||
/*
|
||||
@@ -457,22 +457,22 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
|
||||
*/
|
||||
void luaD_call (lua_State *L, StkId func, int nresults) {
|
||||
lua_CFunction f;
|
||||
TValue *funcv = s2v(func);
|
||||
switch (ttypetag(funcv)) {
|
||||
case LUA_TCCL: /* C closure */
|
||||
f = clCvalue(funcv)->f;
|
||||
retry:
|
||||
switch (ttypetag(s2v(func))) {
|
||||
case LUA_VCCL: /* C closure */
|
||||
f = clCvalue(s2v(func))->f;
|
||||
goto Cfunc;
|
||||
case LUA_TLCF: /* light C function */
|
||||
f = fvalue(funcv);
|
||||
case LUA_VLCF: /* light C function */
|
||||
f = fvalue(s2v(func));
|
||||
Cfunc: {
|
||||
int n; /* number of returns */
|
||||
CallInfo *ci;
|
||||
CallInfo *ci = next_ci(L);
|
||||
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
|
||||
ci = next_ci(L);
|
||||
ci->nresults = nresults;
|
||||
ci->callstatus = CIST_C;
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
ci->func = func;
|
||||
L->ci = ci;
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
if (L->hookmask & LUA_MASKCALL) {
|
||||
int narg = cast_int(L->top - func) - 1;
|
||||
@@ -485,19 +485,19 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
|
||||
luaD_poscall(L, ci, n);
|
||||
break;
|
||||
}
|
||||
case LUA_TLCL: { /* Lua function */
|
||||
CallInfo *ci;
|
||||
Proto *p = clLvalue(funcv)->p;
|
||||
case LUA_VLCL: { /* Lua function */
|
||||
CallInfo *ci = next_ci(L);
|
||||
Proto *p = clLvalue(s2v(func))->p;
|
||||
int narg = cast_int(L->top - func) - 1; /* number of real arguments */
|
||||
int nfixparams = p->numparams;
|
||||
int fsize = p->maxstacksize; /* frame size */
|
||||
checkstackp(L, fsize, func);
|
||||
ci = next_ci(L);
|
||||
ci->nresults = nresults;
|
||||
ci->u.l.savedpc = p->code; /* starting point */
|
||||
ci->callstatus = 0;
|
||||
ci->top = func + 1 + fsize;
|
||||
ci->func = func;
|
||||
L->ci = ci;
|
||||
for (; narg < nfixparams; narg++)
|
||||
setnilvalue(s2v(L->top++)); /* complete missing arguments */
|
||||
lua_assert(ci->top <= L->stack_last);
|
||||
@@ -505,9 +505,9 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
|
||||
break;
|
||||
}
|
||||
default: { /* not a function */
|
||||
checkstackp(L, 1, func); /* space for metamethod */
|
||||
luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */
|
||||
luaD_call(L, func, nresults); /* now it must be a function */
|
||||
break;
|
||||
goto retry; /* try again with metamethod */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -521,7 +521,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
|
||||
*/
|
||||
void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
|
||||
incXCcalls(L);
|
||||
if (getCcalls(L) >= LUAI_MAXCSTACK) /* possible stack overflow? */
|
||||
if (getCcalls(L) <= CSTACKERR) /* possible stack overflow? */
|
||||
luaE_freeCI(L);
|
||||
luaD_call(L, func, nResults);
|
||||
decXCcalls(L);
|
||||
@@ -672,10 +672,10 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
|
||||
else if (L->status != LUA_YIELD) /* ended with errors? */
|
||||
return resume_error(L, "cannot resume dead coroutine", nargs);
|
||||
if (from == NULL)
|
||||
L->nCcalls = 1;
|
||||
L->nCcalls = CSTACKTHREAD;
|
||||
else /* correct 'nCcalls' for this thread */
|
||||
L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF;
|
||||
if (L->nCcalls >= LUAI_MAXCSTACK)
|
||||
L->nCcalls = getCcalls(from) + from->nci - L->nci - CSTACKCF;
|
||||
if (L->nCcalls <= CSTACKERR)
|
||||
return resume_error(L, "C stack overflow", nargs);
|
||||
luai_userstateresume(L, nargs);
|
||||
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
|
||||
|
||||
170
ldump.c
170
ldump.c
@@ -29,15 +29,15 @@ typedef struct {
|
||||
|
||||
|
||||
/*
|
||||
** All high-level dumps go through DumpVector; you can change it to
|
||||
** All high-level dumps go through dumpVector; you can change it to
|
||||
** change the endianness of the result
|
||||
*/
|
||||
#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
|
||||
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
|
||||
|
||||
#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
|
||||
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
|
||||
|
||||
|
||||
static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
||||
static void dumpBlock (DumpState *D, const void *b, size_t size) {
|
||||
if (D->status == 0 && size > 0) {
|
||||
lua_unlock(D->L);
|
||||
D->status = (*D->writer)(D->L, b, size, D->data);
|
||||
@@ -46,19 +46,19 @@ static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
||||
}
|
||||
|
||||
|
||||
#define DumpVar(x,D) DumpVector(&x,1,D)
|
||||
#define dumpVar(D,x) dumpVector(D,&x,1)
|
||||
|
||||
|
||||
static void DumpByte (int y, DumpState *D) {
|
||||
static void dumpByte (DumpState *D, int y) {
|
||||
lu_byte x = (lu_byte)y;
|
||||
DumpVar(x, D);
|
||||
dumpVar(D, x);
|
||||
}
|
||||
|
||||
|
||||
/* DumpInt Buff Size */
|
||||
/* dumpInt Buff Size */
|
||||
#define DIBS ((sizeof(size_t) * 8 / 7) + 1)
|
||||
|
||||
static void DumpSize (size_t x, DumpState *D) {
|
||||
static void dumpSize (DumpState *D, size_t x) {
|
||||
lu_byte buff[DIBS];
|
||||
int n = 0;
|
||||
do {
|
||||
@@ -66,146 +66,144 @@ static void DumpSize (size_t x, DumpState *D) {
|
||||
x >>= 7;
|
||||
} while (x != 0);
|
||||
buff[DIBS - 1] |= 0x80; /* mark last byte */
|
||||
DumpVector(buff + DIBS - n, n, D);
|
||||
dumpVector(D, buff + DIBS - n, n);
|
||||
}
|
||||
|
||||
|
||||
static void DumpInt (int x, DumpState *D) {
|
||||
DumpSize(x, D);
|
||||
static void dumpInt (DumpState *D, int x) {
|
||||
dumpSize(D, x);
|
||||
}
|
||||
|
||||
|
||||
static void DumpNumber (lua_Number x, DumpState *D) {
|
||||
DumpVar(x, D);
|
||||
static void dumpNumber (DumpState *D, lua_Number x) {
|
||||
dumpVar(D, x);
|
||||
}
|
||||
|
||||
|
||||
static void DumpInteger (lua_Integer x, DumpState *D) {
|
||||
DumpVar(x, D);
|
||||
static void dumpInteger (DumpState *D, lua_Integer x) {
|
||||
dumpVar(D, x);
|
||||
}
|
||||
|
||||
|
||||
static void DumpString (const TString *s, DumpState *D) {
|
||||
static void dumpString (DumpState *D, const TString *s) {
|
||||
if (s == NULL)
|
||||
DumpSize(0, D);
|
||||
dumpSize(D, 0);
|
||||
else {
|
||||
size_t size = tsslen(s);
|
||||
const char *str = getstr(s);
|
||||
DumpSize(size + 1, D);
|
||||
DumpVector(str, size, D);
|
||||
dumpSize(D, size + 1);
|
||||
dumpVector(D, str, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void DumpCode (const Proto *f, DumpState *D) {
|
||||
DumpInt(f->sizecode, D);
|
||||
DumpVector(f->code, f->sizecode, D);
|
||||
static void dumpCode (DumpState *D, const Proto *f) {
|
||||
dumpInt(D, f->sizecode);
|
||||
dumpVector(D, f->code, f->sizecode);
|
||||
}
|
||||
|
||||
|
||||
static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
|
||||
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
|
||||
|
||||
static void DumpConstants (const Proto *f, DumpState *D) {
|
||||
static void dumpConstants (DumpState *D, const Proto *f) {
|
||||
int i;
|
||||
int n = f->sizek;
|
||||
DumpInt(n, D);
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
const TValue *o = &f->k[i];
|
||||
DumpByte(ttypetag(o), D);
|
||||
switch (ttypetag(o)) {
|
||||
case LUA_TNIL:
|
||||
int tt = ttypetag(o);
|
||||
dumpByte(D, tt);
|
||||
switch (tt) {
|
||||
case LUA_VNUMFLT:
|
||||
dumpNumber(D, fltvalue(o));
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
DumpByte(bvalue(o), D);
|
||||
case LUA_VNUMINT:
|
||||
dumpInteger(D, ivalue(o));
|
||||
break;
|
||||
case LUA_TNUMFLT:
|
||||
DumpNumber(fltvalue(o), D);
|
||||
case LUA_VSHRSTR:
|
||||
case LUA_VLNGSTR:
|
||||
dumpString(D, tsvalue(o));
|
||||
break;
|
||||
case LUA_TNUMINT:
|
||||
DumpInteger(ivalue(o), D);
|
||||
break;
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR:
|
||||
DumpString(tsvalue(o), D);
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
default:
|
||||
lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void DumpProtos (const Proto *f, DumpState *D) {
|
||||
static void dumpProtos (DumpState *D, const Proto *f) {
|
||||
int i;
|
||||
int n = f->sizep;
|
||||
DumpInt(n, D);
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++)
|
||||
DumpFunction(f->p[i], f->source, D);
|
||||
dumpFunction(D, f->p[i], f->source);
|
||||
}
|
||||
|
||||
|
||||
static void DumpUpvalues (const Proto *f, DumpState *D) {
|
||||
static void dumpUpvalues (DumpState *D, const Proto *f) {
|
||||
int i, n = f->sizeupvalues;
|
||||
DumpInt(n, D);
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
DumpByte(f->upvalues[i].instack, D);
|
||||
DumpByte(f->upvalues[i].idx, D);
|
||||
dumpByte(D, f->upvalues[i].instack);
|
||||
dumpByte(D, f->upvalues[i].idx);
|
||||
dumpByte(D, f->upvalues[i].kind);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void DumpDebug (const Proto *f, DumpState *D) {
|
||||
static void dumpDebug (DumpState *D, const Proto *f) {
|
||||
int i, n;
|
||||
n = (D->strip) ? 0 : f->sizelineinfo;
|
||||
DumpInt(n, D);
|
||||
DumpVector(f->lineinfo, n, D);
|
||||
dumpInt(D, n);
|
||||
dumpVector(D, f->lineinfo, n);
|
||||
n = (D->strip) ? 0 : f->sizeabslineinfo;
|
||||
DumpInt(n, D);
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
DumpInt(f->abslineinfo[i].pc, D);
|
||||
DumpInt(f->abslineinfo[i].line, D);
|
||||
dumpInt(D, f->abslineinfo[i].pc);
|
||||
dumpInt(D, f->abslineinfo[i].line);
|
||||
}
|
||||
n = (D->strip) ? 0 : f->sizelocvars;
|
||||
DumpInt(n, D);
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
DumpString(f->locvars[i].varname, D);
|
||||
DumpInt(f->locvars[i].startpc, D);
|
||||
DumpInt(f->locvars[i].endpc, D);
|
||||
dumpString(D, f->locvars[i].varname);
|
||||
dumpInt(D, f->locvars[i].startpc);
|
||||
dumpInt(D, f->locvars[i].endpc);
|
||||
}
|
||||
n = (D->strip) ? 0 : f->sizeupvalues;
|
||||
DumpInt(n, D);
|
||||
dumpInt(D, n);
|
||||
for (i = 0; i < n; i++)
|
||||
DumpString(f->upvalues[i].name, D);
|
||||
dumpString(D, f->upvalues[i].name);
|
||||
}
|
||||
|
||||
|
||||
static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
|
||||
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
|
||||
if (D->strip || f->source == psource)
|
||||
DumpString(NULL, D); /* no debug info or same source as its parent */
|
||||
dumpString(D, NULL); /* no debug info or same source as its parent */
|
||||
else
|
||||
DumpString(f->source, D);
|
||||
DumpInt(f->linedefined, D);
|
||||
DumpInt(f->lastlinedefined, D);
|
||||
DumpByte(f->numparams, D);
|
||||
DumpByte(f->is_vararg, D);
|
||||
DumpByte(f->maxstacksize, D);
|
||||
DumpCode(f, D);
|
||||
DumpConstants(f, D);
|
||||
DumpUpvalues(f, D);
|
||||
DumpProtos(f, D);
|
||||
DumpDebug(f, D);
|
||||
dumpString(D, f->source);
|
||||
dumpInt(D, f->linedefined);
|
||||
dumpInt(D, f->lastlinedefined);
|
||||
dumpByte(D, f->numparams);
|
||||
dumpByte(D, f->is_vararg);
|
||||
dumpByte(D, f->maxstacksize);
|
||||
dumpCode(D, f);
|
||||
dumpConstants(D, f);
|
||||
dumpUpvalues(D, f);
|
||||
dumpProtos(D, f);
|
||||
dumpDebug(D, f);
|
||||
}
|
||||
|
||||
|
||||
static void DumpHeader (DumpState *D) {
|
||||
DumpLiteral(LUA_SIGNATURE, D);
|
||||
DumpInt(LUAC_VERSION, D);
|
||||
DumpByte(LUAC_FORMAT, D);
|
||||
DumpLiteral(LUAC_DATA, D);
|
||||
DumpByte(sizeof(Instruction), D);
|
||||
DumpByte(sizeof(lua_Integer), D);
|
||||
DumpByte(sizeof(lua_Number), D);
|
||||
DumpInteger(LUAC_INT, D);
|
||||
DumpNumber(LUAC_NUM, D);
|
||||
static void dumpHeader (DumpState *D) {
|
||||
dumpLiteral(D, LUA_SIGNATURE);
|
||||
dumpByte(D, LUAC_VERSION);
|
||||
dumpByte(D, LUAC_FORMAT);
|
||||
dumpLiteral(D, LUAC_DATA);
|
||||
dumpByte(D, sizeof(Instruction));
|
||||
dumpByte(D, sizeof(lua_Integer));
|
||||
dumpByte(D, sizeof(lua_Number));
|
||||
dumpInteger(D, LUAC_INT);
|
||||
dumpNumber(D, LUAC_NUM);
|
||||
}
|
||||
|
||||
|
||||
@@ -220,9 +218,9 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
|
||||
D.data = data;
|
||||
D.strip = strip;
|
||||
D.status = 0;
|
||||
DumpHeader(&D);
|
||||
DumpByte(f->sizeupvalues, &D);
|
||||
DumpFunction(f, NULL, &D);
|
||||
dumpHeader(&D);
|
||||
dumpByte(&D, f->sizeupvalues);
|
||||
dumpFunction(&D, f, NULL);
|
||||
return D.status;
|
||||
}
|
||||
|
||||
|
||||
113
lfunc.c
113
lfunc.c
@@ -24,20 +24,20 @@
|
||||
|
||||
|
||||
|
||||
CClosure *luaF_newCclosure (lua_State *L, int n) {
|
||||
GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
|
||||
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
|
||||
GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
|
||||
CClosure *c = gco2ccl(o);
|
||||
c->nupvalues = cast_byte(n);
|
||||
c->nupvalues = cast_byte(nupvals);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
LClosure *luaF_newLclosure (lua_State *L, int n) {
|
||||
GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
|
||||
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
|
||||
GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
|
||||
LClosure *c = gco2lcl(o);
|
||||
c->p = NULL;
|
||||
c->nupvalues = cast_byte(n);
|
||||
while (n--) c->upvals[n] = NULL;
|
||||
c->nupvalues = cast_byte(nupvals);
|
||||
while (nupvals--) c->upvals[nupvals] = NULL;
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ LClosure *luaF_newLclosure (lua_State *L, int n) {
|
||||
void luaF_initupvals (lua_State *L, LClosure *cl) {
|
||||
int i;
|
||||
for (i = 0; i < cl->nupvalues; i++) {
|
||||
GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal));
|
||||
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
|
||||
UpVal *uv = gco2upv(o);
|
||||
uv->v = &uv->u.value; /* make it closed */
|
||||
setnilvalue(uv->v);
|
||||
@@ -59,14 +59,15 @@ void luaF_initupvals (lua_State *L, LClosure *cl) {
|
||||
|
||||
|
||||
/*
|
||||
** Create a new upvalue with the given tag at the given level,
|
||||
** and link it to the list of open upvalues of 'L' after entry 'prev'.
|
||||
** Create a new upvalue at the given level, and link it to the list of
|
||||
** open upvalues of 'L' after entry 'prev'.
|
||||
**/
|
||||
static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) {
|
||||
GCObject *o = luaC_newobj(L, tag, sizeof(UpVal));
|
||||
static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) {
|
||||
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
|
||||
UpVal *uv = gco2upv(o);
|
||||
UpVal *next = *prev;
|
||||
uv->v = s2v(level); /* current value lives in the stack */
|
||||
uv->tbc = tbc;
|
||||
uv->u.open.next = next; /* link it to list of open upvalues */
|
||||
uv->u.open.previous = prev;
|
||||
if (next)
|
||||
@@ -81,7 +82,7 @@ static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) {
|
||||
|
||||
|
||||
/*
|
||||
** Find and reuse, or create if it does not exist, a regular upvalue
|
||||
** Find and reuse, or create if it does not exist, an upvalue
|
||||
** at the given level.
|
||||
*/
|
||||
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||
@@ -89,12 +90,13 @@ UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||
UpVal *p;
|
||||
lua_assert(isintwups(L) || L->openupval == NULL);
|
||||
while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
|
||||
if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */
|
||||
lua_assert(!isdead(G(L), p));
|
||||
if (uplevel(p) == level) /* corresponding upvalue? */
|
||||
return p; /* return it */
|
||||
pp = &p->u.open.next;
|
||||
}
|
||||
/* not found: create a new upvalue after 'pp' */
|
||||
return newupval(L, LUA_TUPVAL, level, pp);
|
||||
return newupval(L, 0, level, pp);
|
||||
}
|
||||
|
||||
|
||||
@@ -121,39 +123,53 @@ static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Raise an error with message 'msg', inserting the name of the
|
||||
** local variable at position 'level' in the stack.
|
||||
*/
|
||||
static void varerror (lua_State *L, StkId level, const char *msg) {
|
||||
int idx = cast_int(level - L->ci->func);
|
||||
const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
|
||||
if (vname == NULL) vname = "?";
|
||||
luaG_runerror(L, msg, vname);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Prepare and call a closing method. If status is OK, code is still
|
||||
** inside the original protected call, and so any error will be handled
|
||||
** there. Otherwise, a previous error already activated original
|
||||
** there. Otherwise, a previous error already activated the original
|
||||
** protected call, and so the call to the closing method must be
|
||||
** protected here. (A status = CLOSEPROTECT behaves like a previous
|
||||
** protected here. (A status == CLOSEPROTECT behaves like a previous
|
||||
** error, to also run the closing method in protected mode).
|
||||
** If status is OK, the call to the closing method will be pushed
|
||||
** at the top of the stack. Otherwise, values are pushed after
|
||||
** the 'level' of the upvalue being closed, as everything after
|
||||
** that won't be used again.
|
||||
*/
|
||||
static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) {
|
||||
static int callclosemth (lua_State *L, StkId level, int status) {
|
||||
TValue *uv = s2v(level); /* value being closed */
|
||||
if (likely(status == LUA_OK)) {
|
||||
if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */
|
||||
callclose(L, NULL); /* call closing method */
|
||||
else if (!ttisnil(uv)) { /* non-closable non-nil value? */
|
||||
int idx = cast_int(level - L->ci->func);
|
||||
const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
|
||||
if (vname == NULL) vname = "?";
|
||||
luaG_runerror(L, "attempt to close non-closable variable '%s'", vname);
|
||||
}
|
||||
else if (!l_isfalse(uv)) /* non-closable non-false value? */
|
||||
varerror(L, level, "attempt to close non-closable variable '%s'");
|
||||
}
|
||||
else { /* must close the object in protected mode */
|
||||
ptrdiff_t oldtop = savestack(L, level + 1);
|
||||
/* save error message and set stack top to 'level + 1' */
|
||||
luaD_seterrorobj(L, status, level);
|
||||
ptrdiff_t oldtop;
|
||||
level++; /* space for error message */
|
||||
oldtop = savestack(L, level + 1); /* top will be after that */
|
||||
luaD_seterrorobj(L, status, level); /* set error message */
|
||||
if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */
|
||||
int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0);
|
||||
if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */
|
||||
status = newstatus; /* this will be the new error */
|
||||
else /* leave original error (or nil) on top */
|
||||
else {
|
||||
if (newstatus != LUA_OK) /* suppressed error? */
|
||||
luaE_warnerror(L, "__close metamethod");
|
||||
/* leave original error (or nil) on top */
|
||||
L->top = restorestack(L, oldtop);
|
||||
}
|
||||
}
|
||||
/* else no metamethod; ignore this case and keep original error */
|
||||
}
|
||||
@@ -166,9 +182,7 @@ static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) {
|
||||
** (can raise a memory-allocation error)
|
||||
*/
|
||||
static void trynewtbcupval (lua_State *L, void *ud) {
|
||||
StkId level = cast(StkId, ud);
|
||||
lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
|
||||
newupval(L, LUA_TUPVALTBC, level, &L->openupval);
|
||||
newupval(L, 1, cast(StkId, ud), &L->openupval);
|
||||
}
|
||||
|
||||
|
||||
@@ -178,13 +192,22 @@ static void trynewtbcupval (lua_State *L, void *ud) {
|
||||
** as there is no upvalue to call it later.
|
||||
*/
|
||||
void luaF_newtbcupval (lua_State *L, StkId level) {
|
||||
int status = luaD_rawrunprotected(L, trynewtbcupval, level);
|
||||
if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
|
||||
lua_assert(status == LUA_ERRMEM);
|
||||
luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
|
||||
if (prepclosingmethod(L, s2v(level), s2v(level + 1)))
|
||||
TValue *obj = s2v(level);
|
||||
lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
|
||||
if (!l_isfalse(obj)) { /* false doesn't need to be closed */
|
||||
int status;
|
||||
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
|
||||
if (ttisnil(tm)) /* no metamethod? */
|
||||
varerror(L, level, "variable '%s' got a non-closable value");
|
||||
status = luaD_rawrunprotected(L, trynewtbcupval, level);
|
||||
if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
|
||||
lua_assert(status == LUA_ERRMEM);
|
||||
luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
|
||||
/* next call must succeed, as object is closable */
|
||||
prepclosingmethod(L, s2v(level), s2v(level + 1));
|
||||
callclose(L, NULL); /* call closing method */
|
||||
luaD_throw(L, LUA_ERRMEM); /* throw memory error */
|
||||
luaD_throw(L, LUA_ERRMEM); /* throw memory error */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,27 +223,27 @@ void luaF_unlinkupval (UpVal *uv) {
|
||||
int luaF_close (lua_State *L, StkId level, int status) {
|
||||
UpVal *uv;
|
||||
while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
|
||||
StkId upl = uplevel(uv);
|
||||
TValue *slot = &uv->u.value; /* new position for value */
|
||||
lua_assert(uplevel(uv) < L->top);
|
||||
if (uv->tbc && status != NOCLOSINGMETH) {
|
||||
/* must run closing method, which may change the stack */
|
||||
ptrdiff_t levelrel = savestack(L, level);
|
||||
status = callclosemth(L, uplevel(uv), status);
|
||||
level = restorestack(L, levelrel);
|
||||
}
|
||||
luaF_unlinkupval(uv);
|
||||
setobj(L, slot, uv->v); /* move value to upvalue slot */
|
||||
uv->v = slot; /* now current value lives here */
|
||||
if (!iswhite(uv))
|
||||
gray2black(uv); /* closed upvalues cannot be gray */
|
||||
luaC_barrier(L, uv, slot);
|
||||
if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) {
|
||||
/* must run closing method */
|
||||
ptrdiff_t levelrel = savestack(L, level);
|
||||
status = callclosemth(L, uv->v, upl, status); /* may change the stack */
|
||||
level = restorestack(L, levelrel);
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
Proto *luaF_newproto (lua_State *L) {
|
||||
GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
|
||||
GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
|
||||
Proto *f = gco2p(o);
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
|
||||
4
lfunc.h
4
lfunc.h
@@ -54,8 +54,8 @@
|
||||
|
||||
|
||||
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
||||
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
|
||||
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
|
||||
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals);
|
||||
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals);
|
||||
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
|
||||
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
|
||||
|
||||
124
lgc.c
124
lgc.c
@@ -119,12 +119,12 @@ static void entersweep (lua_State *L);
|
||||
|
||||
static GCObject **getgclist (GCObject *o) {
|
||||
switch (o->tt) {
|
||||
case LUA_TTABLE: return &gco2t(o)->gclist;
|
||||
case LUA_TLCL: return &gco2lcl(o)->gclist;
|
||||
case LUA_TCCL: return &gco2ccl(o)->gclist;
|
||||
case LUA_TTHREAD: return &gco2th(o)->gclist;
|
||||
case LUA_TPROTO: return &gco2p(o)->gclist;
|
||||
case LUA_TUSERDATA: {
|
||||
case LUA_VTABLE: return &gco2t(o)->gclist;
|
||||
case LUA_VLCL: return &gco2lcl(o)->gclist;
|
||||
case LUA_VCCL: return &gco2ccl(o)->gclist;
|
||||
case LUA_VTHREAD: return &gco2th(o)->gclist;
|
||||
case LUA_VPROTO: return &gco2p(o)->gclist;
|
||||
case LUA_VUSERDATA: {
|
||||
Udata *u = gco2u(o);
|
||||
lua_assert(u->nuvalue > 0);
|
||||
return &u->gclist;
|
||||
@@ -152,7 +152,7 @@ static GCObject **getgclist (GCObject *o) {
|
||||
** and its key is not marked, mark its entry as dead. This allows the
|
||||
** collection of the key, but keeps its entry in the table (its removal
|
||||
** could break a chain). The main feature of a dead key is that it must
|
||||
** be different from any other value, to do not disturb searches.
|
||||
** be different from any other value, to do not disturb searches.
|
||||
** Other places never manipulate dead keys, because its associated empty
|
||||
** value is enough to signal that the entry is logically empty.
|
||||
*/
|
||||
@@ -268,20 +268,19 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
|
||||
static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
white2gray(o);
|
||||
switch (o->tt) {
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR: {
|
||||
case LUA_VSHRSTR:
|
||||
case LUA_VLNGSTR: {
|
||||
gray2black(o);
|
||||
break;
|
||||
}
|
||||
case LUA_TUPVAL:
|
||||
case LUA_TUPVALTBC: {
|
||||
case LUA_VUPVAL: {
|
||||
UpVal *uv = gco2upv(o);
|
||||
if (!upisopen(uv)) /* open upvalues are kept gray */
|
||||
gray2black(o);
|
||||
markvalue(g, uv->v); /* mark its content */
|
||||
break;
|
||||
}
|
||||
case LUA_TUSERDATA: {
|
||||
case LUA_VUSERDATA: {
|
||||
Udata *u = gco2u(o);
|
||||
if (u->nuvalue == 0) { /* no user values? */
|
||||
markobjectN(g, u->metatable); /* mark its metatable */
|
||||
@@ -290,8 +289,8 @@ static void reallymarkobject (global_State *g, GCObject *o) {
|
||||
}
|
||||
/* else... */
|
||||
} /* FALLTHROUGH */
|
||||
case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE:
|
||||
case LUA_TTHREAD: case LUA_TPROTO: {
|
||||
case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE:
|
||||
case LUA_VTHREAD: case LUA_VPROTO: {
|
||||
linkobjgclist(o, g->gray);
|
||||
break;
|
||||
}
|
||||
@@ -414,13 +413,13 @@ static void traverseweakvalue (global_State *g, Table *h) {
|
||||
** (in the atomic phase). In generational mode, it (like all visited
|
||||
** tables) must be kept in some gray list for post-processing.
|
||||
*/
|
||||
static int traverseephemeron (global_State *g, Table *h) {
|
||||
static int traverseephemeron (global_State *g, Table *h, int inv) {
|
||||
int marked = 0; /* true if an object is marked in this traversal */
|
||||
int hasclears = 0; /* true if table has white keys */
|
||||
int hasww = 0; /* true if table has entry "white-key -> white-value" */
|
||||
Node *n, *limit = gnodelast(h);
|
||||
unsigned int i;
|
||||
unsigned int asize = luaH_realasize(h);
|
||||
unsigned int nsize = sizenode(h);
|
||||
/* traverse array part */
|
||||
for (i = 0; i < asize; i++) {
|
||||
if (valiswhite(&h->array[i])) {
|
||||
@@ -428,8 +427,10 @@ static int traverseephemeron (global_State *g, Table *h) {
|
||||
reallymarkobject(g, gcvalue(&h->array[i]));
|
||||
}
|
||||
}
|
||||
/* traverse hash part */
|
||||
for (n = gnode(h, 0); n < limit; n++) {
|
||||
/* traverse hash part; if 'inv', traverse descending
|
||||
(see 'convergeephemerons') */
|
||||
for (i = 0; i < nsize; i++) {
|
||||
Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i);
|
||||
if (isempty(gval(n))) /* entry is empty? */
|
||||
clearkey(n); /* clear its key */
|
||||
else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */
|
||||
@@ -491,7 +492,7 @@ static lu_mem traversetable (global_State *g, Table *h) {
|
||||
if (!weakkey) /* strong keys? */
|
||||
traverseweakvalue(g, h);
|
||||
else if (!weakvalue) /* strong values? */
|
||||
traverseephemeron(g, h);
|
||||
traverseephemeron(g, h, 0);
|
||||
else /* all weak */
|
||||
linkgclist(h, g->allweak); /* nothing to traverse now */
|
||||
}
|
||||
@@ -570,10 +571,8 @@ static int traversethread (global_State *g, lua_State *th) {
|
||||
th->openupval == NULL || isintwups(th));
|
||||
for (; o < th->top; o++) /* mark live elements in the stack */
|
||||
markvalue(g, s2v(o));
|
||||
for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) {
|
||||
if (uv->tt == LUA_TUPVALTBC) /* to be closed? */
|
||||
markobject(g, uv); /* cannot be collected */
|
||||
}
|
||||
for (uv = th->openupval; uv != NULL; uv = uv->u.open.next)
|
||||
markobject(g, uv); /* open upvalues cannot be collected */
|
||||
if (g->gcstate == GCSatomic) { /* final traversal? */
|
||||
StkId lim = th->stack + th->stacksize; /* real end of stack */
|
||||
for (; o < lim; o++) /* clear not-marked stack slice */
|
||||
@@ -599,12 +598,12 @@ static lu_mem propagatemark (global_State *g) {
|
||||
gray2black(o);
|
||||
g->gray = *getgclist(o); /* remove from 'gray' list */
|
||||
switch (o->tt) {
|
||||
case LUA_TTABLE: return traversetable(g, gco2t(o));
|
||||
case LUA_TUSERDATA: return traverseudata(g, gco2u(o));
|
||||
case LUA_TLCL: return traverseLclosure(g, gco2lcl(o));
|
||||
case LUA_TCCL: return traverseCclosure(g, gco2ccl(o));
|
||||
case LUA_TPROTO: return traverseproto(g, gco2p(o));
|
||||
case LUA_TTHREAD: {
|
||||
case LUA_VTABLE: return traversetable(g, gco2t(o));
|
||||
case LUA_VUSERDATA: return traverseudata(g, gco2u(o));
|
||||
case LUA_VLCL: return traverseLclosure(g, gco2lcl(o));
|
||||
case LUA_VCCL: return traverseCclosure(g, gco2ccl(o));
|
||||
case LUA_VPROTO: return traverseproto(g, gco2p(o));
|
||||
case LUA_VTHREAD: {
|
||||
lua_State *th = gco2th(o);
|
||||
linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
|
||||
black2gray(o);
|
||||
@@ -623,21 +622,30 @@ static lu_mem propagateall (global_State *g) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse all ephemeron tables propagating marks from keys to values.
|
||||
** Repeat until it converges, that is, nothing new is marked. 'dir'
|
||||
** inverts the direction of the traversals, trying to speed up
|
||||
** convergence on chains in the same table.
|
||||
**
|
||||
*/
|
||||
static void convergeephemerons (global_State *g) {
|
||||
int changed;
|
||||
int dir = 0;
|
||||
do {
|
||||
GCObject *w;
|
||||
GCObject *next = g->ephemeron; /* get ephemeron list */
|
||||
g->ephemeron = NULL; /* tables may return to this list when traversed */
|
||||
changed = 0;
|
||||
while ((w = next) != NULL) {
|
||||
next = gco2t(w)->gclist;
|
||||
if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */
|
||||
while ((w = next) != NULL) { /* for each ephemeron table */
|
||||
next = gco2t(w)->gclist; /* list is rebuilt during loop */
|
||||
if (traverseephemeron(g, gco2t(w), dir)) { /* marked some value? */
|
||||
propagateall(g); /* propagate changes */
|
||||
changed = 1; /* will have to revisit all ephemeron tables */
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
dir = !dir; /* invert direction next time */
|
||||
} while (changed); /* repeat until no more changes */
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
@@ -702,35 +710,34 @@ static void freeupval (lua_State *L, UpVal *uv) {
|
||||
|
||||
static void freeobj (lua_State *L, GCObject *o) {
|
||||
switch (o->tt) {
|
||||
case LUA_TPROTO:
|
||||
case LUA_VPROTO:
|
||||
luaF_freeproto(L, gco2p(o));
|
||||
break;
|
||||
case LUA_TUPVAL:
|
||||
case LUA_TUPVALTBC:
|
||||
case LUA_VUPVAL:
|
||||
freeupval(L, gco2upv(o));
|
||||
break;
|
||||
case LUA_TLCL:
|
||||
case LUA_VLCL:
|
||||
luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues));
|
||||
break;
|
||||
case LUA_TCCL:
|
||||
case LUA_VCCL:
|
||||
luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues));
|
||||
break;
|
||||
case LUA_TTABLE:
|
||||
case LUA_VTABLE:
|
||||
luaH_free(L, gco2t(o));
|
||||
break;
|
||||
case LUA_TTHREAD:
|
||||
case LUA_VTHREAD:
|
||||
luaE_freethread(L, gco2th(o));
|
||||
break;
|
||||
case LUA_TUSERDATA: {
|
||||
case LUA_VUSERDATA: {
|
||||
Udata *u = gco2u(o);
|
||||
luaM_freemem(L, o, sizeudata(u->nuvalue, u->len));
|
||||
break;
|
||||
}
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_VSHRSTR:
|
||||
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
|
||||
luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen));
|
||||
break;
|
||||
case LUA_TLNGSTR:
|
||||
case LUA_VLNGSTR:
|
||||
luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen));
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
@@ -794,10 +801,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) {
|
||||
*/
|
||||
static void checkSizes (lua_State *L, global_State *g) {
|
||||
if (!g->gcemergency) {
|
||||
l_mem olddebt = g->GCdebt;
|
||||
if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
|
||||
if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */
|
||||
l_mem olddebt = g->GCdebt;
|
||||
luaS_resize(L, g->strt.size / 2);
|
||||
g->GCestimate += g->GCdebt - olddebt; /* correct estimate */
|
||||
g->GCestimate += g->GCdebt - olddebt; /* correct estimate */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -838,21 +846,16 @@ static void GCTM (lua_State *L) {
|
||||
int running = g->gcrunning;
|
||||
L->allowhook = 0; /* stop debug hooks during GC metamethod */
|
||||
g->gcrunning = 0; /* avoid GC steps */
|
||||
setobj2s(L, L->top, tm); /* push finalizer... */
|
||||
setobj2s(L, L->top + 1, &v); /* ... and its argument */
|
||||
L->top += 2; /* and (next line) call the finalizer */
|
||||
setobj2s(L, L->top++, tm); /* push finalizer... */
|
||||
setobj2s(L, L->top++, &v); /* ... and its argument */
|
||||
L->ci->callstatus |= CIST_FIN; /* will run a finalizer */
|
||||
status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
|
||||
L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */
|
||||
L->allowhook = oldah; /* restore hooks */
|
||||
g->gcrunning = running; /* restore state */
|
||||
if (status != LUA_OK) { /* error while running __gc? */
|
||||
const char *msg = (ttisstring(s2v(L->top - 1)))
|
||||
? svalue(s2v(L->top - 1))
|
||||
: "error object is not a string";
|
||||
luaE_warning(L, "error in __gc metamethod (", 1);
|
||||
luaE_warning(L, msg, 1);
|
||||
luaE_warning(L, ")", 0);
|
||||
if (unlikely(status != LUA_OK)) { /* error while running __gc? */
|
||||
luaE_warnerror(L, "__gc metamethod");
|
||||
L->top--; /* pops error object */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -995,7 +998,7 @@ static void sweep2old (lua_State *L, GCObject **p) {
|
||||
*/
|
||||
static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
|
||||
GCObject *limit) {
|
||||
static lu_byte nextage[] = {
|
||||
static const lu_byte nextage[] = {
|
||||
G_SURVIVAL, /* from G_NEW */
|
||||
G_OLD1, /* from G_SURVIVAL */
|
||||
G_OLD1, /* from G_OLD0 */
|
||||
@@ -1046,7 +1049,7 @@ static GCObject **correctgraylist (GCObject **p) {
|
||||
GCObject *curr;
|
||||
while ((curr = *p) != NULL) {
|
||||
switch (curr->tt) {
|
||||
case LUA_TTABLE: case LUA_TUSERDATA: {
|
||||
case LUA_VTABLE: case LUA_VUSERDATA: {
|
||||
GCObject **next = getgclist(curr);
|
||||
if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */
|
||||
lua_assert(isgray(curr));
|
||||
@@ -1066,7 +1069,7 @@ static GCObject **correctgraylist (GCObject **p) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
case LUA_VTHREAD: {
|
||||
lua_State *th = gco2th(curr);
|
||||
lua_assert(!isblack(th));
|
||||
if (iswhite(th)) /* new object? */
|
||||
@@ -1250,7 +1253,7 @@ static void setminordebt (global_State *g) {
|
||||
/*
|
||||
** Does a major collection after last collection was a "bad collection".
|
||||
**
|
||||
** When the program is building a big struture, it allocates lots of
|
||||
** When the program is building a big structure, it allocates lots of
|
||||
** memory but generates very little garbage. In those scenarios,
|
||||
** the generational mode just wastes time doing small collections, and
|
||||
** major collections are frequently what we call a "bad collection", a
|
||||
@@ -1562,6 +1565,7 @@ static void incstep (lua_State *L, global_State *g) {
|
||||
*/
|
||||
void luaC_step (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(!g->gcemergency);
|
||||
if (g->gcrunning) { /* running? */
|
||||
if(isdecGCmodegen(g))
|
||||
genstep(L, g);
|
||||
|
||||
47
liolib.c
47
liolib.c
@@ -153,7 +153,7 @@ static int io_type (lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
||||
if (p == NULL)
|
||||
lua_pushnil(L); /* not a file */
|
||||
luaL_pushfail(L); /* not a file */
|
||||
else if (isclosed(p))
|
||||
lua_pushliteral(L, "closed file");
|
||||
else
|
||||
@@ -215,7 +215,7 @@ static int f_close (lua_State *L) {
|
||||
|
||||
static int io_close (lua_State *L) {
|
||||
if (lua_isnone(L, 1)) /* no argument? */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */
|
||||
return f_close(L);
|
||||
}
|
||||
|
||||
@@ -270,6 +270,7 @@ static int io_open (lua_State *L) {
|
||||
*/
|
||||
static int io_pclose (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
errno = 0;
|
||||
return luaL_execresult(L, l_pclose(L, p->f));
|
||||
}
|
||||
|
||||
@@ -296,7 +297,7 @@ static FILE *getiofile (lua_State *L, const char *findex) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
||||
p = (LStream *)lua_touserdata(L, -1);
|
||||
if (isclosed(p))
|
||||
luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
|
||||
luaL_error(L, "default %s file is closed", findex + IOPREF_LEN);
|
||||
return p->f;
|
||||
}
|
||||
|
||||
@@ -338,7 +339,7 @@ static int io_readline (lua_State *L);
|
||||
#define MAXARGLINE 250
|
||||
|
||||
/*
|
||||
** Auxiliar function to create the iteration function for 'lines'.
|
||||
** Auxiliary function to create the iteration function for 'lines'.
|
||||
** The iteration function is a closure over 'io_readline', with
|
||||
** the following upvalues:
|
||||
** 1) The file being read (first value in the stack)
|
||||
@@ -593,7 +594,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
||||
return luaL_fileresult(L, 0, NULL);
|
||||
if (!success) {
|
||||
lua_pop(L, 1); /* remove last result */
|
||||
lua_pushnil(L); /* push nil instead */
|
||||
luaL_pushfail(L); /* push nil instead */
|
||||
}
|
||||
return n - first;
|
||||
}
|
||||
@@ -626,7 +627,7 @@ static int io_readline (lua_State *L) {
|
||||
lua_assert(n > 0); /* should return at least a nil */
|
||||
if (lua_toboolean(L, -n)) /* read at least one value? */
|
||||
return n; /* return them */
|
||||
else { /* first result is nil: EOF or error */
|
||||
else { /* first result is false: EOF or error */
|
||||
if (n > 1) { /* is there error information? */
|
||||
/* 2nd result is error message */
|
||||
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
||||
@@ -742,14 +743,23 @@ static const luaL_Reg iolib[] = {
|
||||
/*
|
||||
** methods for file handles
|
||||
*/
|
||||
static const luaL_Reg flib[] = {
|
||||
{"close", f_close},
|
||||
{"flush", f_flush},
|
||||
{"lines", f_lines},
|
||||
static const luaL_Reg meth[] = {
|
||||
{"read", f_read},
|
||||
{"seek", f_seek},
|
||||
{"setvbuf", f_setvbuf},
|
||||
{"write", f_write},
|
||||
{"lines", f_lines},
|
||||
{"flush", f_flush},
|
||||
{"seek", f_seek},
|
||||
{"close", f_close},
|
||||
{"setvbuf", f_setvbuf},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** metamethods for file handles
|
||||
*/
|
||||
static const luaL_Reg metameth[] = {
|
||||
{"__index", NULL}, /* place holder */
|
||||
{"__gc", f_gc},
|
||||
{"__close", f_gc},
|
||||
{"__tostring", f_tostring},
|
||||
@@ -758,11 +768,12 @@ static const luaL_Reg flib[] = {
|
||||
|
||||
|
||||
static void createmeta (lua_State *L) {
|
||||
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
||||
lua_pushvalue(L, -1); /* push metatable */
|
||||
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
||||
luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
|
||||
lua_pop(L, 1); /* pop new metatable */
|
||||
luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
|
||||
luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
|
||||
luaL_newlibtable(L, meth); /* create method table */
|
||||
luaL_setfuncs(L, meth, 0); /* add file methods to method table */
|
||||
lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
|
||||
lua_pop(L, 1); /* pop metatable */
|
||||
}
|
||||
|
||||
|
||||
@@ -772,7 +783,7 @@ static void createmeta (lua_State *L) {
|
||||
static int io_noclose (lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
p->closef = &io_noclose; /* keep file opened */
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
lua_pushliteral(L, "cannot close standard file");
|
||||
return 2;
|
||||
}
|
||||
|
||||
15
ljumptab.h
15
ljumptab.h
@@ -16,7 +16,7 @@
|
||||
#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i));
|
||||
|
||||
|
||||
static void *disptab[NUM_OPCODES] = {
|
||||
static const void *const disptab[NUM_OPCODES] = {
|
||||
|
||||
#if 0
|
||||
** you can update the following list with this command:
|
||||
@@ -30,7 +30,9 @@ static void *disptab[NUM_OPCODES] = {
|
||||
&&L_OP_LOADF,
|
||||
&&L_OP_LOADK,
|
||||
&&L_OP_LOADKX,
|
||||
&&L_OP_LOADBOOL,
|
||||
&&L_OP_LOADFALSE,
|
||||
&&L_OP_LFALSESKIP,
|
||||
&&L_OP_LOADTRUE,
|
||||
&&L_OP_LOADNIL,
|
||||
&&L_OP_GETUPVAL,
|
||||
&&L_OP_SETUPVAL,
|
||||
@@ -45,12 +47,6 @@ static void *disptab[NUM_OPCODES] = {
|
||||
&&L_OP_NEWTABLE,
|
||||
&&L_OP_SELF,
|
||||
&&L_OP_ADDI,
|
||||
&&L_OP_SUBI,
|
||||
&&L_OP_MULI,
|
||||
&&L_OP_MODI,
|
||||
&&L_OP_POWI,
|
||||
&&L_OP_DIVI,
|
||||
&&L_OP_IDIVI,
|
||||
&&L_OP_ADDK,
|
||||
&&L_OP_SUBK,
|
||||
&&L_OP_MULK,
|
||||
@@ -75,6 +71,9 @@ static void *disptab[NUM_OPCODES] = {
|
||||
&&L_OP_BXOR,
|
||||
&&L_OP_SHL,
|
||||
&&L_OP_SHR,
|
||||
&&L_OP_MMBIN,
|
||||
&&L_OP_MMBINI,
|
||||
&&L_OP_MMBINK,
|
||||
&&L_OP_UNM,
|
||||
&&L_OP_BNOT,
|
||||
&&L_OP_NOT,
|
||||
|
||||
22
llex.c
22
llex.c
@@ -136,7 +136,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||
if (isempty(o)) { /* not in use yet? */
|
||||
/* boolean value does not need GC barrier;
|
||||
table is not a metatable, so it does not need to invalidate cache */
|
||||
setbvalue(o, 1); /* t[string] = true */
|
||||
setbtvalue(o); /* t[string] = true */
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
else { /* string already present */
|
||||
@@ -211,8 +211,16 @@ static int check_next2 (LexState *ls, const char *set) {
|
||||
|
||||
/* LUA_NUMBER */
|
||||
/*
|
||||
** this function is quite liberal in what it accepts, as 'luaO_str2num'
|
||||
** will reject ill-formed numerals.
|
||||
** This function is quite liberal in what it accepts, as 'luaO_str2num'
|
||||
** will reject ill-formed numerals. Roughly, it accepts the following
|
||||
** pattern:
|
||||
**
|
||||
** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))*
|
||||
**
|
||||
** The only tricky part is to accept [+-] only after a valid exponent
|
||||
** mark, to avoid reading '3-4' or '0xe+1' as a single number.
|
||||
**
|
||||
** The caller might have already read an initial dot.
|
||||
*/
|
||||
static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||
TValue obj;
|
||||
@@ -223,15 +231,13 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
|
||||
expo = "Pp";
|
||||
for (;;) {
|
||||
if (check_next2(ls, expo)) /* exponent part? */
|
||||
if (check_next2(ls, expo)) /* exponent mark? */
|
||||
check_next2(ls, "-+"); /* optional exponent sign */
|
||||
if (lisxdigit(ls->current))
|
||||
save_and_next(ls);
|
||||
else if (ls->current == '.')
|
||||
else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */
|
||||
save_and_next(ls);
|
||||
else break;
|
||||
}
|
||||
if (lislalnum(ls->current)) /* is numeral touching an alpha num? */
|
||||
if (lislalpha(ls->current)) /* is numeral touching a letter? */
|
||||
save_and_next(ls); /* force an error */
|
||||
save(ls, '\0');
|
||||
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
|
||||
|
||||
@@ -99,7 +99,7 @@ typedef LUAI_UACINT l_uacInt;
|
||||
** assertion for checking API calls
|
||||
*/
|
||||
#if !defined(luai_apicheck)
|
||||
#define luai_apicheck(l,e) lua_assert(e)
|
||||
#define luai_apicheck(l,e) ((void)l, lua_assert(e))
|
||||
#endif
|
||||
|
||||
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||
|
||||
51
lmathlib.c
51
lmathlib.c
@@ -77,7 +77,7 @@ static int math_toint (lua_State *L) {
|
||||
lua_pushinteger(L, n);
|
||||
else {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnil(L); /* value is not convertible to integer */
|
||||
luaL_pushfail(L); /* value is not convertible to integer */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -235,7 +235,7 @@ static int math_type (lua_State *L) {
|
||||
lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
|
||||
else {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -249,7 +249,7 @@ static int math_type (lua_State *L) {
|
||||
*/
|
||||
|
||||
/* number of binary digits in the mantissa of a float */
|
||||
#define FIGS l_mathlim(MANT_DIG)
|
||||
#define FIGS l_floatatt(MANT_DIG)
|
||||
|
||||
#if FIGS > 64
|
||||
/* there are only 64 random bits; use them all */
|
||||
@@ -328,7 +328,7 @@ static Rand64 nextrand (Rand64 *state) {
|
||||
*/
|
||||
|
||||
/* must throw out the extra (64 - FIGS) bits */
|
||||
#define shift64_FIG (64 - FIGS)
|
||||
#define shift64_FIG (64 - FIGS)
|
||||
|
||||
/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */
|
||||
#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
|
||||
@@ -522,16 +522,18 @@ typedef struct {
|
||||
** Project the random integer 'ran' into the interval [0, n].
|
||||
** Because 'ran' has 2^B possible values, the projection can only be
|
||||
** uniform when the size of the interval is a power of 2 (exact
|
||||
** division). To get a uniform projection into [0, n], we first compute
|
||||
** 'lim', the smallest Mersenne number not smaller than 'n'. We then
|
||||
** project 'ran' into the interval [0, lim]. If the result is inside
|
||||
** [0, n], we are done. Otherwise, we try with another 'ran', until we
|
||||
** have a result inside the interval.
|
||||
** division). Otherwise, to get a uniform projection into [0, n], we
|
||||
** first compute 'lim', the smallest Mersenne number not smaller than
|
||||
** 'n'. We then project 'ran' into the interval [0, lim]. If the result
|
||||
** is inside [0, n], we are done. Otherwise, we try with another 'ran',
|
||||
** until we have a result inside the interval.
|
||||
*/
|
||||
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
||||
RanState *state) {
|
||||
lua_Unsigned lim = n;
|
||||
if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */
|
||||
if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
|
||||
return ran & n; /* no bias */
|
||||
else {
|
||||
lua_Unsigned lim = n;
|
||||
/* compute the smallest (2^b - 1) not smaller than 'n' */
|
||||
lim |= (lim >> 1);
|
||||
lim |= (lim >> 2);
|
||||
@@ -541,13 +543,13 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
||||
#if (LUA_MAXUNSIGNED >> 31) >= 3
|
||||
lim |= (lim >> 32); /* integer type has more than 32 bits */
|
||||
#endif
|
||||
lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
|
||||
&& lim >= n /* not smaller than 'n', */
|
||||
&& (lim >> 1) < n); /* and it is the smallest one */
|
||||
while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
|
||||
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
|
||||
return ran;
|
||||
}
|
||||
lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */
|
||||
&& lim >= n /* not smaller than 'n', */
|
||||
&& (lim == 0 || (lim >> 1) < n)); /* and it is the smallest one */
|
||||
while ((ran &= lim) > n) /* project 'ran' into [0..lim] */
|
||||
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
|
||||
return ran;
|
||||
}
|
||||
|
||||
|
||||
@@ -586,7 +588,8 @@ static int math_random (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
|
||||
static void setseed (lua_State *L, Rand64 *state,
|
||||
lua_Unsigned n1, lua_Unsigned n2) {
|
||||
int i;
|
||||
state[0] = Int2I(n1);
|
||||
state[1] = Int2I(0xff); /* avoid a zero state */
|
||||
@@ -594,6 +597,8 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
|
||||
state[3] = Int2I(0);
|
||||
for (i = 0; i < 16; i++)
|
||||
nextrand(state); /* discard initial values to "spread" seed */
|
||||
lua_pushinteger(L, n1);
|
||||
lua_pushinteger(L, n2);
|
||||
}
|
||||
|
||||
|
||||
@@ -605,20 +610,21 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {
|
||||
static void randseed (lua_State *L, RanState *state) {
|
||||
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
|
||||
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
|
||||
setseed(state->s, seed1, seed2);
|
||||
setseed(L, state->s, seed1, seed2);
|
||||
}
|
||||
|
||||
|
||||
static int math_randomseed (lua_State *L) {
|
||||
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
||||
if (lua_isnone(L, 1))
|
||||
if (lua_isnone(L, 1)) {
|
||||
randseed(L, state);
|
||||
}
|
||||
else {
|
||||
lua_Integer n1 = luaL_checkinteger(L, 1);
|
||||
lua_Integer n2 = luaL_optinteger(L, 2, 0);
|
||||
setseed(state->s, n1, n2);
|
||||
setseed(L, state->s, n1, n2);
|
||||
}
|
||||
return 0;
|
||||
return 2; /* return seeds */
|
||||
}
|
||||
|
||||
|
||||
@@ -635,6 +641,7 @@ static const luaL_Reg randfuncs[] = {
|
||||
static void setrandfunc (lua_State *L) {
|
||||
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
|
||||
randseed(L, state); /* initialize with a "random" seed */
|
||||
lua_pop(L, 2); /* remove pushed seeds */
|
||||
luaL_setfuncs(L, randfuncs, 1);
|
||||
}
|
||||
|
||||
|
||||
85
lmem.c
85
lmem.c
@@ -23,33 +23,56 @@
|
||||
|
||||
|
||||
#if defined(HARDMEMTESTS)
|
||||
#define hardtest(L,os,s) /* force a GC whenever possible */ \
|
||||
if ((s) > (os) && (G(L))->gcrunning) luaC_fullgc(L, 1);
|
||||
/*
|
||||
** First allocation will fail whenever not building initial state
|
||||
** and not shrinking a block. (This fail will trigger 'tryagain' and
|
||||
** a full GC cycle at every allocation.)
|
||||
*/
|
||||
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
||||
if (ttisnil(&g->nilvalue) && ns > os)
|
||||
return NULL; /* fail */
|
||||
else /* normal allocation */
|
||||
return (*g->frealloc)(g->ud, block, os, ns);
|
||||
}
|
||||
#else
|
||||
#define hardtest(L,os,s) ((void)0)
|
||||
#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** 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)
|
||||
**
|
||||
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
|
||||
** matter 'x').
|
||||
** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
|
||||
** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
|
||||
** which is equivalent to free(NULL) in ISO C.
|
||||
**
|
||||
** * frealloc(ud, p, x, 0) frees the block 'p'
|
||||
** (in this specific case, frealloc must return NULL);
|
||||
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
||||
** (which is equivalent to free(NULL) in ISO C)
|
||||
** - frealloc(ud, NULL, x, s) creates a new block of size 's'
|
||||
** (no matter 'x'). Returns NULL if it cannot create the new block.
|
||||
**
|
||||
** frealloc returns NULL if it cannot create or reallocate the area
|
||||
** (any reallocation to an equal or smaller size cannot fail!)
|
||||
** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
|
||||
** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
|
||||
** block to the new size.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** Functions to allocate/deallocate arrays for the Parser
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
** Minimum size for arrays during parsing, to avoid overhead of
|
||||
** reallocating to size 1, then 2, and then 4. All these arrays
|
||||
** will be reallocated to exact sizes or erased when parsing ends.
|
||||
*/
|
||||
#define MINSIZEARRAY 4
|
||||
|
||||
|
||||
@@ -71,32 +94,32 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
||||
}
|
||||
lua_assert(nelems + 1 <= size && size <= limit);
|
||||
/* 'limit' ensures that multiplication will not overflow */
|
||||
newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems,
|
||||
cast_sizet(size) * size_elems);
|
||||
if (unlikely(newblock == NULL))
|
||||
luaM_error(L);
|
||||
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
|
||||
cast_sizet(size) * size_elems);
|
||||
*psize = size; /* update only when everything else is OK */
|
||||
return newblock;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** In prototypes, the size of the array is also its number of
|
||||
** elements (to save memory). So, if it cannot shrink an array
|
||||
** to its number of elements, the only option is to raise an
|
||||
** error.
|
||||
*/
|
||||
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
||||
int final_n, int size_elem) {
|
||||
global_State *g = G(L);
|
||||
void *newblock;
|
||||
size_t oldsize = cast_sizet((*size) * size_elem);
|
||||
size_t newsize = cast_sizet(final_n * size_elem);
|
||||
lua_assert(newsize <= oldsize);
|
||||
newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
|
||||
if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */
|
||||
luaM_error(L);
|
||||
else {
|
||||
g->GCdebt += newsize - oldsize;
|
||||
*size = final_n;
|
||||
return newblock;
|
||||
}
|
||||
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
||||
*size = final_n;
|
||||
return newblock;
|
||||
}
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
l_noret luaM_toobig (lua_State *L) {
|
||||
luaG_runerror(L, "memory allocation error: block too big");
|
||||
@@ -132,19 +155,20 @@ static void *tryagain (lua_State *L, void *block,
|
||||
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
** Generic allocation routine.
|
||||
** If allocation fails while shrinking a block, do not try again; the
|
||||
** GC shrinks some blocks and it is not reentrant.
|
||||
*/
|
||||
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||
void *newblock;
|
||||
global_State *g = G(L);
|
||||
lua_assert((osize == 0) == (block == NULL));
|
||||
hardtest(L, osize, nsize);
|
||||
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||
newblock = firsttry(g, block, osize, nsize);
|
||||
if (unlikely(newblock == NULL && nsize > 0)) {
|
||||
if (nsize > osize) /* not shrinking a block? */
|
||||
newblock = tryagain(L, block, osize, nsize);
|
||||
if (newblock == NULL) /* still no memory? */
|
||||
return NULL;
|
||||
return NULL; /* do not update 'GCdebt' */
|
||||
}
|
||||
lua_assert((nsize == 0) == (newblock == NULL));
|
||||
g->GCdebt = (g->GCdebt + nsize) - osize;
|
||||
@@ -162,12 +186,11 @@ void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
|
||||
|
||||
|
||||
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
||||
hardtest(L, 0, size);
|
||||
if (size == 0)
|
||||
return NULL; /* that's all */
|
||||
else {
|
||||
global_State *g = G(L);
|
||||
void *newblock = (*g->frealloc)(g->ud, NULL, tag, size);
|
||||
void *newblock = firsttry(g, NULL, tag, size);
|
||||
if (unlikely(newblock == NULL)) {
|
||||
newblock = tryagain(L, NULL, tag, size);
|
||||
if (newblock == NULL)
|
||||
|
||||
46
loadlib.c
46
loadlib.c
@@ -56,10 +56,10 @@
|
||||
|
||||
|
||||
/*
|
||||
** unique key for table in the registry that keeps handles
|
||||
** key for table in the registry that keeps handles
|
||||
** for all loaded C libraries
|
||||
*/
|
||||
static const int CLIBS = 0;
|
||||
static const char *const CLIBS = "_CLIBS";
|
||||
|
||||
#define LIB_FAIL "open"
|
||||
|
||||
@@ -67,6 +67,13 @@ static const int CLIBS = 0;
|
||||
#define setprogdir(L) ((void)0)
|
||||
|
||||
|
||||
/*
|
||||
** Special type equivalent to '(void*)' for functions in gcc
|
||||
** (to suppress warnings when converting function pointers)
|
||||
*/
|
||||
typedef void (*voidf)(void);
|
||||
|
||||
|
||||
/*
|
||||
** system-dependent functions
|
||||
*/
|
||||
@@ -206,7 +213,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
||||
|
||||
|
||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
|
||||
lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
|
||||
if (f == NULL) pusherror(L);
|
||||
return f;
|
||||
}
|
||||
@@ -269,8 +276,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||
#endif
|
||||
|
||||
|
||||
#define AUXMARK "\1" /* auxiliary mark */
|
||||
|
||||
|
||||
/*
|
||||
** return registry.LUA_NOENV as a boolean
|
||||
@@ -308,7 +313,7 @@ static void setpath (lua_State *L, const char *fieldname,
|
||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
||||
}
|
||||
luaL_addstring(&b, dft); /* add default */
|
||||
if (dftmark < path + len - 2) { /* is there a sufix after ';;'? */
|
||||
if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
|
||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
||||
luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
|
||||
}
|
||||
@@ -327,7 +332,7 @@ static void setpath (lua_State *L, const char *fieldname,
|
||||
*/
|
||||
static void *checkclib (lua_State *L, const char *path) {
|
||||
void *plib;
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_getfield(L, -1, path);
|
||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||
@@ -340,7 +345,7 @@ static void *checkclib (lua_State *L, const char *path) {
|
||||
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
||||
*/
|
||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_pushlightuserdata(L, plib);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||
@@ -408,10 +413,10 @@ static int ll_loadlib (lua_State *L) {
|
||||
if (stat == 0) /* no errors? */
|
||||
return 1; /* return the loaded function */
|
||||
else { /* error; error message is on stack top */
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
lua_insert(L, -2);
|
||||
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
||||
return 3; /* return nil, error message, and where */
|
||||
return 3; /* return fail, error message, and where */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,13 +463,13 @@ static const char *getnextfilename (char **path, char *end) {
|
||||
/*
|
||||
** Given a path such as ";blabla.so;blublu.so", pushes the string
|
||||
**
|
||||
** no file 'blabla.so'
|
||||
** no file 'blabla.so'
|
||||
** no file 'blublu.so'
|
||||
*/
|
||||
static void pusherrornotfound (lua_State *L, const char *path) {
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
luaL_addstring(&b, "\n\tno file '");
|
||||
luaL_addstring(&b, "no file '");
|
||||
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
|
||||
luaL_addstring(&b, "'");
|
||||
luaL_pushresult(&b);
|
||||
@@ -505,9 +510,9 @@ static int ll_searchpath (lua_State *L) {
|
||||
luaL_optstring(L, 4, LUA_DIRSEP));
|
||||
if (f != NULL) return 1;
|
||||
else { /* error message is on top of the stack */
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
lua_insert(L, -2);
|
||||
return 2; /* return nil + error message */
|
||||
return 2; /* return fail + error message */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -591,7 +596,7 @@ static int searcher_Croot (lua_State *L) {
|
||||
if (stat != ERRFUNC)
|
||||
return checkload(L, 0, filename); /* real error */
|
||||
else { /* open function not found */
|
||||
lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
|
||||
lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -604,7 +609,7 @@ static int searcher_preload (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||
if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
|
||||
lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
|
||||
lua_pushfstring(L, "no field package.preload['%s']", name);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
@@ -623,8 +628,10 @@ static void findloader (lua_State *L, const char *name) {
|
||||
luaL_buffinit(L, &msg);
|
||||
/* iterate over available searchers to find a loader */
|
||||
for (i = 1; ; i++) {
|
||||
luaL_addstring(&msg, "\n\t"); /* error-message prefix */
|
||||
if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
|
||||
lua_pop(L, 1); /* remove nil */
|
||||
luaL_buffsub(&msg, 2); /* remove prefix */
|
||||
luaL_pushresult(&msg); /* create error message */
|
||||
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
||||
}
|
||||
@@ -636,8 +643,10 @@ static void findloader (lua_State *L, const char *name) {
|
||||
lua_pop(L, 1); /* remove extra return */
|
||||
luaL_addvalue(&msg); /* concatenate error message */
|
||||
}
|
||||
else
|
||||
else { /* no error message */
|
||||
lua_pop(L, 2); /* remove both returns */
|
||||
luaL_buffsub(&msg, 2); /* remove prefix */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -716,12 +725,11 @@ static void createsearcherstable (lua_State *L) {
|
||||
** setting a finalizer to close all libraries when closing state.
|
||||
*/
|
||||
static void createclibstable (lua_State *L) {
|
||||
lua_newtable(L); /* create CLIBS table */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
||||
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||
lua_pushcfunction(L, gctm);
|
||||
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||
lua_setmetatable(L, -2);
|
||||
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
||||
}
|
||||
|
||||
|
||||
|
||||
26
lobject.c
26
lobject.c
@@ -29,32 +29,6 @@
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
/*
|
||||
** converts an integer to a "floating point byte", represented as
|
||||
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
||||
** eeeee != 0 and (xxx) otherwise.
|
||||
*/
|
||||
int luaO_int2fb (unsigned int x) {
|
||||
int e = 0; /* exponent */
|
||||
if (x < 8) return x;
|
||||
while (x >= (8 << 4)) { /* coarse steps */
|
||||
x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
|
||||
e += 4;
|
||||
}
|
||||
while (x >= (8 << 1)) { /* fine steps */
|
||||
x = (x + 1) >> 1; /* x = ceil(x / 2) */
|
||||
e++;
|
||||
}
|
||||
return ((e+1) << 3) | (cast_int(x) - 8);
|
||||
}
|
||||
|
||||
|
||||
/* converts back */
|
||||
int luaO_fb2int (int x) {
|
||||
return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Computes ceil(log2(x))
|
||||
*/
|
||||
|
||||
199
lobject.h
199
lobject.h
@@ -17,24 +17,28 @@
|
||||
|
||||
|
||||
/*
|
||||
** Extra tags for non-values
|
||||
** Extra types for collectable non-values
|
||||
*/
|
||||
#define LUA_TUPVAL LUA_NUMTAGS /* upvalues */
|
||||
#define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */
|
||||
#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */
|
||||
#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */
|
||||
|
||||
|
||||
/*
|
||||
** number of all possible tags (including LUA_TNONE)
|
||||
** number of all possible types (including LUA_TNONE)
|
||||
*/
|
||||
#define LUA_TOTALTAGS (LUA_TPROTO + 2)
|
||||
#define LUA_TOTALTYPES (LUA_TPROTO + 2)
|
||||
|
||||
|
||||
/*
|
||||
** tags for Tagged Values have the following use of bits:
|
||||
** bits 0-3: actual tag (a LUA_T* value)
|
||||
** bits 0-3: actual tag (a LUA_T* constant)
|
||||
** bits 4-5: variant bits
|
||||
** bit 6: whether value is collectable
|
||||
*/
|
||||
|
||||
/* add variant bits to a type */
|
||||
#define makevariant(t,v) ((t) | ((v) << 4))
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ -43,7 +47,6 @@
|
||||
typedef union Value {
|
||||
struct GCObject *gc; /* collectable objects */
|
||||
void *p; /* light userdata */
|
||||
int b; /* booleans */
|
||||
lua_CFunction f; /* light C functions */
|
||||
lua_Integer i; /* integer numbers */
|
||||
lua_Number n; /* float numbers */
|
||||
@@ -86,24 +89,35 @@ typedef struct TValue {
|
||||
|
||||
|
||||
/* Macros for internal tests */
|
||||
|
||||
/* collectable object has the same tag as the original value */
|
||||
#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
|
||||
|
||||
/*
|
||||
** Any value being manipulated by the program either is non
|
||||
** collectable, or the collectable object has the right tag
|
||||
** and it is not dead.
|
||||
*/
|
||||
#define checkliveness(L,obj) \
|
||||
lua_longassert(!iscollectable(obj) || \
|
||||
(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
|
||||
((void)L, lua_longassert(!iscollectable(obj) || \
|
||||
(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
|
||||
|
||||
|
||||
/* Macros to set values */
|
||||
|
||||
/* set a value's tag */
|
||||
#define settt_(o,t) ((o)->tt_=(t))
|
||||
|
||||
|
||||
/* main macro to copy values (from 'obj1' to 'obj2') */
|
||||
#define setobj(L,obj1,obj2) \
|
||||
{ TValue *io1=(obj1); const TValue *io2=(obj2); \
|
||||
io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
|
||||
(void)L; checkliveness(L,io1); lua_assert(!isreallyempty(io1)); }
|
||||
io1->value_ = io2->value_; settt_(io1, io2->tt_); \
|
||||
checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
|
||||
|
||||
/*
|
||||
** different types of assignments, according to destination
|
||||
** Different types of assignments, according to source and destination.
|
||||
** (They are mostly equal now, but may be different in the future.)
|
||||
*/
|
||||
|
||||
/* from stack to stack */
|
||||
@@ -118,13 +132,16 @@ typedef struct TValue {
|
||||
#define setobj2t setobj
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Entries in the Lua stack
|
||||
*/
|
||||
typedef union StackValue {
|
||||
TValue val;
|
||||
} StackValue;
|
||||
|
||||
|
||||
typedef StackValue *StkId; /* index to stack elements */
|
||||
/* index to stack elements */
|
||||
typedef StackValue *StkId;
|
||||
|
||||
/* convert a 'StackValue' to a 'TValue' */
|
||||
#define s2v(o) (&(o)->val)
|
||||
@@ -137,36 +154,34 @@ typedef StackValue *StkId; /* index to stack elements */
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
/* Standard nil */
|
||||
#define LUA_VNIL makevariant(LUA_TNIL, 0)
|
||||
|
||||
/* Empty slot (which might be different from a slot containing nil) */
|
||||
#define LUA_VEMPTY makevariant(LUA_TNIL, 1)
|
||||
|
||||
/* Value returned for a key not found in a table (absent key) */
|
||||
#define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
|
||||
|
||||
|
||||
/* macro to test for (any kind of) nil */
|
||||
#define ttisnil(v) checktype((v), LUA_TNIL)
|
||||
|
||||
/* macro to test for a "pure" nil */
|
||||
#define ttisstrictnil(o) checktag((o), LUA_TNIL)
|
||||
|
||||
/* macro to test for a standard nil */
|
||||
#define ttisstrictnil(o) checktag((o), LUA_VNIL)
|
||||
|
||||
|
||||
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
|
||||
#define setnilvalue(obj) settt_(obj, LUA_VNIL)
|
||||
|
||||
|
||||
/*
|
||||
** Variant tag, used only in tables to signal an empty slot
|
||||
** (which might be different from a slot containing nil)
|
||||
*/
|
||||
#define LUA_TEMPTY (LUA_TNIL | (1 << 4))
|
||||
|
||||
/*
|
||||
** Variant used only in the value returned for a key not found in a
|
||||
** table (absent key).
|
||||
*/
|
||||
#define LUA_TABSTKEY (LUA_TNIL | (2 << 4))
|
||||
|
||||
|
||||
#define isabstkey(v) checktag((v), LUA_TABSTKEY)
|
||||
#define isabstkey(v) checktag((v), LUA_VABSTKEY)
|
||||
|
||||
|
||||
/*
|
||||
** macro to detect non-standard nils (used only in assertions)
|
||||
*/
|
||||
#define isreallyempty(v) (ttisnil(v) && !ttisstrictnil(v))
|
||||
#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))
|
||||
|
||||
|
||||
/*
|
||||
@@ -178,11 +193,11 @@ typedef StackValue *StkId; /* index to stack elements */
|
||||
|
||||
|
||||
/* macro defining a value corresponding to an absent key */
|
||||
#define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY
|
||||
#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
|
||||
|
||||
|
||||
/* mark an entry as empty */
|
||||
#define setempty(v) settt_(v, LUA_TEMPTY)
|
||||
#define setempty(v) settt_(v, LUA_VEMPTY)
|
||||
|
||||
|
||||
|
||||
@@ -195,16 +210,20 @@ typedef StackValue *StkId; /* index to stack elements */
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
|
||||
|
||||
#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
|
||||
#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
|
||||
#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
|
||||
|
||||
#define bvalueraw(v) ((v).b)
|
||||
#define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
|
||||
#define ttisfalse(o) checktag((o), LUA_VFALSE)
|
||||
#define ttistrue(o) checktag((o), LUA_VTRUE)
|
||||
|
||||
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
|
||||
|
||||
#define setbvalue(obj,x) \
|
||||
{ TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
|
||||
#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
|
||||
|
||||
|
||||
#define setbfvalue(obj) settt_(obj, LUA_VFALSE)
|
||||
#define setbtvalue(obj) settt_(obj, LUA_VTRUE)
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
@@ -215,13 +234,15 @@ typedef StackValue *StkId; /* index to stack elements */
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
|
||||
#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)
|
||||
|
||||
#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
|
||||
|
||||
#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
|
||||
|
||||
#define setthvalue(L,obj,x) \
|
||||
{ TValue *io = (obj); lua_State *x_ = (x); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
|
||||
checkliveness(L,io); }
|
||||
|
||||
#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
|
||||
@@ -274,12 +295,12 @@ typedef struct GCObject {
|
||||
*/
|
||||
|
||||
/* Variant tags for numbers */
|
||||
#define LUA_TNUMFLT (LUA_TNUMBER | (1 << 4)) /* float numbers */
|
||||
#define LUA_TNUMINT (LUA_TNUMBER | (2 << 4)) /* integer numbers */
|
||||
#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */
|
||||
#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */
|
||||
|
||||
#define ttisnumber(o) checktype((o), LUA_TNUMBER)
|
||||
#define ttisfloat(o) checktag((o), LUA_TNUMFLT)
|
||||
#define ttisinteger(o) checktag((o), LUA_TNUMINT)
|
||||
#define ttisfloat(o) checktag((o), LUA_VNUMFLT)
|
||||
#define ttisinteger(o) checktag((o), LUA_VNUMINT)
|
||||
|
||||
#define nvalue(o) check_exp(ttisnumber(o), \
|
||||
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
|
||||
@@ -290,13 +311,13 @@ typedef struct GCObject {
|
||||
#define ivalueraw(v) ((v).i)
|
||||
|
||||
#define setfltvalue(obj,x) \
|
||||
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); }
|
||||
{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
|
||||
|
||||
#define chgfltvalue(obj,x) \
|
||||
{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
|
||||
|
||||
#define setivalue(obj,x) \
|
||||
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); }
|
||||
{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
|
||||
|
||||
#define chgivalue(obj,x) \
|
||||
{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
|
||||
@@ -311,12 +332,12 @@ typedef struct GCObject {
|
||||
*/
|
||||
|
||||
/* Variant tags for strings */
|
||||
#define LUA_TSHRSTR (LUA_TSTRING | (1 << 4)) /* short strings */
|
||||
#define LUA_TLNGSTR (LUA_TSTRING | (2 << 4)) /* long strings */
|
||||
#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */
|
||||
#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */
|
||||
|
||||
#define ttisstring(o) checktype((o), LUA_TSTRING)
|
||||
#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
|
||||
#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
|
||||
#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))
|
||||
#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))
|
||||
|
||||
#define tsvalueraw(v) (gco2ts((v).gc))
|
||||
|
||||
@@ -335,8 +356,7 @@ typedef struct GCObject {
|
||||
|
||||
|
||||
/*
|
||||
** Header for string value; string bytes follow the end of this structure
|
||||
** (aligned according to 'UTString'; see next).
|
||||
** Header for a string value.
|
||||
*/
|
||||
typedef struct TString {
|
||||
CommonHeader;
|
||||
@@ -347,23 +367,22 @@ typedef struct TString {
|
||||
size_t lnglen; /* length for long strings */
|
||||
struct TString *hnext; /* linked list for hash table */
|
||||
} u;
|
||||
char contents[1];
|
||||
} TString;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Get the actual string (array of bytes) from a 'TString'.
|
||||
** (Access to 'extra' ensures that value is really a 'TString'.)
|
||||
*/
|
||||
#define getstr(ts) \
|
||||
check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString))
|
||||
#define getstr(ts) ((ts)->contents)
|
||||
|
||||
|
||||
/* get the actual string (array of bytes) from a Lua value */
|
||||
#define svalue(o) getstr(tsvalue(o))
|
||||
|
||||
/* get string length from 'TString *s' */
|
||||
#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen)
|
||||
#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen)
|
||||
|
||||
/* get string length from 'TValue *o' */
|
||||
#define vslen(o) tsslen(tsvalue(o))
|
||||
@@ -377,8 +396,17 @@ typedef struct TString {
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
|
||||
#define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA)
|
||||
|
||||
/*
|
||||
** Light userdata should be a variant of userdata, but for compatibility
|
||||
** reasons they are also different types.
|
||||
*/
|
||||
#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)
|
||||
|
||||
#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)
|
||||
|
||||
#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
|
||||
#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
|
||||
|
||||
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
|
||||
#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
|
||||
@@ -386,11 +414,11 @@ typedef struct TString {
|
||||
#define pvalueraw(v) ((v).p)
|
||||
|
||||
#define setpvalue(obj,x) \
|
||||
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
|
||||
{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
|
||||
|
||||
#define setuvalue(L,obj,x) \
|
||||
{ TValue *io = (obj); Udata *x_ = (x); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
|
||||
checkliveness(L,io); }
|
||||
|
||||
|
||||
@@ -453,6 +481,9 @@ typedef struct Udata0 {
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define LUA_VPROTO makevariant(LUA_TPROTO, 0)
|
||||
|
||||
|
||||
/*
|
||||
** Description of an upvalue for function prototypes
|
||||
*/
|
||||
@@ -460,6 +491,7 @@ typedef struct Upvaldesc {
|
||||
TString *name; /* upvalue name (for debug information) */
|
||||
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 kind; /* kind of corresponding variable */
|
||||
} Upvaldesc;
|
||||
|
||||
|
||||
@@ -526,16 +558,19 @@ typedef struct Proto {
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)
|
||||
|
||||
|
||||
/* Variant tags for functions */
|
||||
#define LUA_TLCL (LUA_TFUNCTION | (1 << 4)) /* Lua closure */
|
||||
#define LUA_TLCF (LUA_TFUNCTION | (2 << 4)) /* light C function */
|
||||
#define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */
|
||||
#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */
|
||||
#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */
|
||||
#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */
|
||||
|
||||
#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
|
||||
#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL)
|
||||
#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
|
||||
#define ttislcf(o) checktag((o), LUA_TLCF)
|
||||
#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
|
||||
#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL)
|
||||
#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
|
||||
#define ttislcf(o) checktag((o), LUA_VLCF)
|
||||
#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
|
||||
|
||||
#define isLfunction(o) ttisLclosure(o)
|
||||
|
||||
@@ -548,17 +583,17 @@ typedef struct Proto {
|
||||
|
||||
#define setclLvalue(L,obj,x) \
|
||||
{ TValue *io = (obj); LClosure *x_ = (x); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
|
||||
checkliveness(L,io); }
|
||||
|
||||
#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
|
||||
|
||||
#define setfvalue(obj,x) \
|
||||
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
|
||||
{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
|
||||
|
||||
#define setclCvalue(L,obj,x) \
|
||||
{ TValue *io = (obj); CClosure *x_ = (x); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
|
||||
checkliveness(L,io); }
|
||||
|
||||
|
||||
@@ -567,6 +602,7 @@ typedef struct Proto {
|
||||
*/
|
||||
typedef struct UpVal {
|
||||
CommonHeader;
|
||||
lu_byte tbc; /* true if it represents a to-be-closed variable */
|
||||
TValue *v; /* points to stack or to its own value */
|
||||
union {
|
||||
struct { /* (when open) */
|
||||
@@ -578,9 +614,6 @@ typedef struct UpVal {
|
||||
} UpVal;
|
||||
|
||||
|
||||
/* variant for "To Be Closed" upvalues */
|
||||
#define LUA_TUPVALTBC (LUA_TUPVAL | (1 << 4))
|
||||
|
||||
|
||||
#define ClosureHeader \
|
||||
CommonHeader; lu_byte nupvalues; GCObject *gclist
|
||||
@@ -616,13 +649,15 @@ typedef union Closure {
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
|
||||
#define LUA_VTABLE makevariant(LUA_TTABLE, 0)
|
||||
|
||||
#define ttistable(o) checktag((o), ctb(LUA_VTABLE))
|
||||
|
||||
#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
|
||||
|
||||
#define sethvalue(L,obj,x) \
|
||||
{ TValue *io = (obj); Table *x_ = (x); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
|
||||
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
|
||||
checkliveness(L,io); }
|
||||
|
||||
#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
|
||||
@@ -650,14 +685,14 @@ typedef union Node {
|
||||
#define setnodekey(L,node,obj) \
|
||||
{ Node *n_=(node); const TValue *io_=(obj); \
|
||||
n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
|
||||
(void)L; checkliveness(L,io_); }
|
||||
checkliveness(L,io_); }
|
||||
|
||||
|
||||
/* copy a value from a key */
|
||||
#define getnodekey(L,obj,node) \
|
||||
{ TValue *io_=(obj); const Node *n_=(node); \
|
||||
io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
|
||||
(void)L; checkliveness(L,io_); }
|
||||
checkliveness(L,io_); }
|
||||
|
||||
|
||||
/*
|
||||
@@ -693,9 +728,9 @@ typedef struct Table {
|
||||
#define keyval(node) ((node)->u.key_val)
|
||||
|
||||
#define keyisnil(node) (keytt(node) == LUA_TNIL)
|
||||
#define keyisinteger(node) (keytt(node) == LUA_TNUMINT)
|
||||
#define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
|
||||
#define keyival(node) (keyval(node).i)
|
||||
#define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR))
|
||||
#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
|
||||
#define keystrval(node) (gco2ts(keyval(node).gc))
|
||||
|
||||
#define setnilkey(node) (keytt(node) = LUA_TNIL)
|
||||
@@ -733,8 +768,6 @@ typedef struct Table {
|
||||
/* size of buffer for 'luaO_utf8esc' function */
|
||||
#define UTF8BUFFSZ 8
|
||||
|
||||
LUAI_FUNC int luaO_int2fb (unsigned int x);
|
||||
LUAI_FUNC int luaO_fb2int (int x);
|
||||
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
|
||||
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
|
||||
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
|
||||
|
||||
171
lopcodes.c
171
lopcodes.c
@@ -10,98 +10,95 @@
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
/* ORDER OP */
|
||||
|
||||
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
/* OT IT T A mode opcode */
|
||||
opmode(0, 0, 0, 1, iABC) /* OP_MOVE */
|
||||
,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */
|
||||
,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */
|
||||
,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */
|
||||
,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETI */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SELF */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_MULI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_MODI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_POWI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_ADDK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SUBK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_MULK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_MODK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_POWK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_DIVK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_IDIVK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BORK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_ADD */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SUB */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_MUL */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_MOD */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_POW */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_DIV */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BAND */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BOR */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHL */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_SHR */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_UNM */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_NOT */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_LEN */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_TBC */
|
||||
,opmode(0, 0, 0, 0, isJ) /* OP_JMP */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_EQ */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_LT */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_LE */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_EQK */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_EQI */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_LTI */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_LEI */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_GTI */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_GEI */
|
||||
,opmode(0, 0, 1, 0, iABC) /* OP_TEST */
|
||||
,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */
|
||||
,opmode(1, 1, 0, 1, iABC) /* OP_CALL */
|
||||
,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */
|
||||
,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */
|
||||
,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */
|
||||
,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */
|
||||
,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */
|
||||
,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */
|
||||
,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */
|
||||
,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */
|
||||
,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */
|
||||
,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */
|
||||
,opmode(0, 0, 0, 1, iABC) /* OP_VARARGPREP */
|
||||
,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */
|
||||
/* MM OT IT T A mode opcode */
|
||||
opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */
|
||||
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
|
||||
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/
|
||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */
|
||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */
|
||||
,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */
|
||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */
|
||||
,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */
|
||||
,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */
|
||||
,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */
|
||||
,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */
|
||||
,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
|
||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
|
||||
,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */
|
||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
|
||||
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
|
||||
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
|
||||
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
|
||||
};
|
||||
|
||||
|
||||
224
lopcodes.h
224
lopcodes.h
@@ -17,11 +17,11 @@
|
||||
|
||||
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
|
||||
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
|
||||
iABC C(8) | B(8) |k| A(8) | Op(7) |
|
||||
iABx Bx(17) | A(8) | Op(7) |
|
||||
iAsB sBx (signed)(17) | A(8) | Op(7) |
|
||||
iAx Ax(25) | Op(7) |
|
||||
isJ sJ(25) | Op(7) |
|
||||
iABC C(8) | B(8) |k| A(8) | Op(7) |
|
||||
iABx Bx(17) | A(8) | Op(7) |
|
||||
iAsBx sBx (signed)(17) | A(8) | Op(7) |
|
||||
iAx Ax(25) | Op(7) |
|
||||
isJ sJ(25) | Op(7) |
|
||||
|
||||
A signed argument is represented in excess K: the represented value is
|
||||
the written unsigned value minus K, where K is half the maximum for the
|
||||
@@ -97,6 +97,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||
#define OFFSET_sC (MAXARG_C >> 1)
|
||||
|
||||
#define int2sC(i) ((i) + OFFSET_sC)
|
||||
#define sC2int(i) ((i) - OFFSET_sC)
|
||||
|
||||
|
||||
/* creates a mask with 'n' 1 bits at position 'p' */
|
||||
#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
|
||||
@@ -123,14 +126,14 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
|
||||
|
||||
#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
|
||||
#define GETARG_sB(i) (GETARG_B(i) - OFFSET_sC)
|
||||
#define GETARG_sB(i) sC2int(GETARG_B(i))
|
||||
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
|
||||
|
||||
#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
|
||||
#define GETARG_sC(i) (GETARG_C(i) - OFFSET_sC)
|
||||
#define GETARG_sC(i) sC2int(GETARG_C(i))
|
||||
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
|
||||
|
||||
#define TESTARG_k(i) (cast_int(((i) & (1u << POS_k))))
|
||||
#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
|
||||
#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
|
||||
#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
|
||||
|
||||
@@ -180,9 +183,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
|
||||
|
||||
/*
|
||||
** R(x) - register
|
||||
** K(x) - constant (in constant table)
|
||||
** RK(x) == if k(i) then K(x) else R(x)
|
||||
** R[x] - register
|
||||
** K[x] - constant (in constant table)
|
||||
** RK(x) == if k(i) then K[x] else R[x]
|
||||
*/
|
||||
|
||||
|
||||
@@ -192,115 +195,115 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
|
||||
typedef enum {
|
||||
/*----------------------------------------------------------------------
|
||||
name args description
|
||||
name args description
|
||||
------------------------------------------------------------------------*/
|
||||
OP_MOVE,/* A B R(A) := R(B) */
|
||||
OP_LOADI,/* A sBx R(A) := sBx */
|
||||
OP_LOADF,/* A sBx R(A) := (lua_Number)sBx */
|
||||
OP_LOADK,/* A Bx R(A) := K(Bx) */
|
||||
OP_LOADKX,/* A R(A) := K(extra arg) */
|
||||
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
|
||||
OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
|
||||
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
|
||||
OP_SETUPVAL,/* A B UpValue[B] := R(A) */
|
||||
OP_MOVE,/* A B R[A] := R[B] */
|
||||
OP_LOADI,/* A sBx R[A] := sBx */
|
||||
OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
|
||||
OP_LOADK,/* A Bx R[A] := K[Bx] */
|
||||
OP_LOADKX,/* A R[A] := K[extra arg] */
|
||||
OP_LOADFALSE,/* A R[A] := false */
|
||||
OP_LFALSESKIP,/*A R[A] := false; pc++ */
|
||||
OP_LOADTRUE,/* A R[A] := true */
|
||||
OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
|
||||
OP_GETUPVAL,/* A B R[A] := UpValue[B] */
|
||||
OP_SETUPVAL,/* A B UpValue[B] := R[A] */
|
||||
|
||||
OP_GETTABUP,/* A B C R(A) := UpValue[B][K(C):string] */
|
||||
OP_GETTABLE,/* A B C R(A) := R(B)[R(C)] */
|
||||
OP_GETI,/* A B C R(A) := R(B)[C] */
|
||||
OP_GETFIELD,/* A B C R(A) := R(B)[K(C):string] */
|
||||
OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */
|
||||
OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */
|
||||
OP_GETI,/* A B C R[A] := R[B][C] */
|
||||
OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */
|
||||
|
||||
OP_SETTABUP,/* A B C UpValue[A][K(B):string] := RK(C) */
|
||||
OP_SETTABLE,/* A B C R(A)[R(B)] := RK(C) */
|
||||
OP_SETI,/* A B C R(A)[B] := RK(C) */
|
||||
OP_SETFIELD,/* A B C R(A)[K(B):string] := RK(C) */
|
||||
OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */
|
||||
OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
|
||||
OP_SETI,/* A B C R[A][B] := RK(C) */
|
||||
OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */
|
||||
|
||||
OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
|
||||
OP_NEWTABLE,/* A B C k R[A] := {} */
|
||||
|
||||
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */
|
||||
OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
|
||||
|
||||
OP_ADDI,/* A B sC R(A) := R(B) + C */
|
||||
OP_SUBI,/* A B sC R(A) := R(B) - C */
|
||||
OP_MULI,/* A B sC R(A) := R(B) * C */
|
||||
OP_MODI,/* A B sC R(A) := R(B) % C */
|
||||
OP_POWI,/* A B sC R(A) := R(B) ^ C */
|
||||
OP_DIVI,/* A B sC R(A) := R(B) / C */
|
||||
OP_IDIVI,/* A B sC R(A) := R(B) // C */
|
||||
OP_ADDI,/* A B sC R[A] := R[B] + sC */
|
||||
|
||||
OP_ADDK,/* A B C R(A) := R(B) + K(C) */
|
||||
OP_SUBK,/* A B C R(A) := R(B) - K(C) */
|
||||
OP_MULK,/* A B C R(A) := R(B) * K(C) */
|
||||
OP_MODK,/* A B C R(A) := R(B) % K(C) */
|
||||
OP_POWK,/* A B C R(A) := R(B) ^ K(C) */
|
||||
OP_DIVK,/* A B C R(A) := R(B) / K(C) */
|
||||
OP_IDIVK,/* A B C R(A) := R(B) // K(C) */
|
||||
OP_ADDK,/* A B C R[A] := R[B] + K[C] */
|
||||
OP_SUBK,/* A B C R[A] := R[B] - K[C] */
|
||||
OP_MULK,/* A B C R[A] := R[B] * K[C] */
|
||||
OP_MODK,/* A B C R[A] := R[B] % K[C] */
|
||||
OP_POWK,/* A B C R[A] := R[B] ^ K[C] */
|
||||
OP_DIVK,/* A B C R[A] := R[B] / K[C] */
|
||||
OP_IDIVK,/* A B C R[A] := R[B] // K[C] */
|
||||
|
||||
OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */
|
||||
OP_BORK,/* A B C R(A) := R(B) | K(C):integer */
|
||||
OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */
|
||||
OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
|
||||
OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
|
||||
OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
|
||||
|
||||
OP_SHRI,/* A B sC R(A) := R(B) >> C */
|
||||
OP_SHLI,/* A B sC R(A) := C << R(B) */
|
||||
OP_SHRI,/* A B sC R[A] := R[B] >> sC */
|
||||
OP_SHLI,/* A B sC R[A] := sC << R[B] */
|
||||
|
||||
OP_ADD,/* A B C R(A) := R(B) + R(C) */
|
||||
OP_SUB,/* A B C R(A) := R(B) - R(C) */
|
||||
OP_MUL,/* A B C R(A) := R(B) * R(C) */
|
||||
OP_MOD,/* A B C R(A) := R(B) % R(C) */
|
||||
OP_POW,/* A B C R(A) := R(B) ^ R(C) */
|
||||
OP_DIV,/* A B C R(A) := R(B) / R(C) */
|
||||
OP_IDIV,/* A B C R(A) := R(B) // R(C) */
|
||||
OP_ADD,/* A B C R[A] := R[B] + R[C] */
|
||||
OP_SUB,/* A B C R[A] := R[B] - R[C] */
|
||||
OP_MUL,/* A B C R[A] := R[B] * R[C] */
|
||||
OP_MOD,/* A B C R[A] := R[B] % R[C] */
|
||||
OP_POW,/* A B C R[A] := R[B] ^ R[C] */
|
||||
OP_DIV,/* A B C R[A] := R[B] / R[C] */
|
||||
OP_IDIV,/* A B C R[A] := R[B] // R[C] */
|
||||
|
||||
OP_BAND,/* A B C R(A) := R(B) & R(C) */
|
||||
OP_BOR,/* A B C R(A) := R(B) | R(C) */
|
||||
OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */
|
||||
OP_SHL,/* A B C R(A) := R(B) << R(C) */
|
||||
OP_SHR,/* A B C R(A) := R(B) >> R(C) */
|
||||
OP_BAND,/* A B C R[A] := R[B] & R[C] */
|
||||
OP_BOR,/* A B C R[A] := R[B] | R[C] */
|
||||
OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
|
||||
OP_SHL,/* A B C R[A] := R[B] << R[C] */
|
||||
OP_SHR,/* A B C R[A] := R[B] >> R[C] */
|
||||
|
||||
OP_UNM,/* A B R(A) := -R(B) */
|
||||
OP_BNOT,/* A B R(A) := ~R(B) */
|
||||
OP_NOT,/* A B R(A) := not R(B) */
|
||||
OP_LEN,/* A B R(A) := length of R(B) */
|
||||
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */
|
||||
OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
|
||||
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
|
||||
|
||||
OP_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */
|
||||
OP_UNM,/* A B R[A] := -R[B] */
|
||||
OP_BNOT,/* A B R[A] := ~R[B] */
|
||||
OP_NOT,/* A B R[A] := not R[B] */
|
||||
OP_LEN,/* A B R[A] := length of R[B] */
|
||||
|
||||
OP_CLOSE,/* A close all upvalues >= R(A) */
|
||||
OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
|
||||
|
||||
OP_CLOSE,/* A close all upvalues >= R[A] */
|
||||
OP_TBC,/* A mark variable A "to be closed" */
|
||||
OP_JMP,/* k sJ pc += sJ (k is used in code generation) */
|
||||
OP_EQ,/* A B if ((R(A) == R(B)) ~= k) then pc++ */
|
||||
OP_LT,/* A B if ((R(A) < R(B)) ~= k) then pc++ */
|
||||
OP_LE,/* A B if ((R(A) <= R(B)) ~= k) then pc++ */
|
||||
OP_JMP,/* sJ pc += sJ */
|
||||
OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */
|
||||
OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */
|
||||
OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
|
||||
|
||||
OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */
|
||||
OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */
|
||||
OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */
|
||||
OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */
|
||||
OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */
|
||||
OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */
|
||||
OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */
|
||||
OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */
|
||||
OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */
|
||||
OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */
|
||||
OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
|
||||
OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
|
||||
|
||||
OP_TEST,/* A if (not R(A) == k) then pc++ */
|
||||
OP_TESTSET,/* A B if (not R(B) == k) then pc++ else R(A) := R(B) */
|
||||
OP_TEST,/* A k if (not R[A] == k) then pc++ */
|
||||
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
|
||||
|
||||
OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
|
||||
OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
|
||||
OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
|
||||
OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
|
||||
|
||||
OP_RETURN,/* A B C return R(A), ... ,R(A+B-2) (see note) */
|
||||
OP_RETURN0,/* return */
|
||||
OP_RETURN1,/* A return R(A) */
|
||||
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
|
||||
OP_RETURN0,/* return */
|
||||
OP_RETURN1,/* A return R[A] */
|
||||
|
||||
OP_FORLOOP,/* A Bx R(A)+=R(A+2);
|
||||
if R(A) <?= R(A+1) then { pc-=Bx; R(A+3)=R(A) } */
|
||||
OP_FORPREP,/* A Bx R(A)-=R(A+2); pc+=Bx */
|
||||
OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */
|
||||
OP_FORPREP,/* A Bx <check values and prepare counters>;
|
||||
if not to run then pc+=Bx+1; */
|
||||
|
||||
OP_TFORPREP,/* A Bx create upvalue for R(A + 3); pc+=Bx */
|
||||
OP_TFORCALL,/* A C R(A+4), ... ,R(A+3+C) := R(A)(R(A+1), R(A+2)); */
|
||||
OP_TFORLOOP,/* A Bx if R(A+2) ~= nil then { R(A)=R(A+2); pc -= Bx } */
|
||||
OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */
|
||||
OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
|
||||
OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
|
||||
|
||||
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
|
||||
OP_SETLIST,/* A B C k R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */
|
||||
|
||||
OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
|
||||
OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
|
||||
|
||||
OP_VARARG,/* A C R(A), R(A+1), ..., R(A+C-2) = vararg */
|
||||
OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
|
||||
|
||||
OP_VARARGPREP,/*A (adjust vararg parameters) */
|
||||
OP_VARARGPREP,/*A (adjust vararg parameters) */
|
||||
|
||||
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
} OpCode;
|
||||
@@ -321,24 +324,33 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
|
||||
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
||||
|
||||
(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then
|
||||
next 'instruction' is EXTRAARG(real C).
|
||||
(*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
|
||||
OP_EXTRAARG.
|
||||
|
||||
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
||||
(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
|
||||
real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
|
||||
bits of C).
|
||||
|
||||
(*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
|
||||
power of 2) plus 1, or zero for size zero. If not k, the array size
|
||||
is C. Otherwise, the array size is EXTRAARG _ C.
|
||||
|
||||
(*) For comparisons, k specifies what condition the test should accept
|
||||
(true or false).
|
||||
|
||||
(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
|
||||
(the constant is the first operand).
|
||||
|
||||
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||
|
||||
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
|
||||
function either builds upvalues, which may need to be closed, or is
|
||||
vararg, which must be corrected before returning. When 'k' is true,
|
||||
C > 0 means the function is vararg and (C - 1) is its number of
|
||||
fixed parameters.
|
||||
function builds upvalues, which may need to be closed. C > 0 means
|
||||
the function is vararg, so that its 'func' must be corrected before
|
||||
returning; in this case, (C - 1) is its number of fixed parameters.
|
||||
|
||||
(*) In comparisons with an immediate operand, C signals whether the
|
||||
original operand was a float.
|
||||
original operand was a float. (It must be corrected in case of
|
||||
metamethods.)
|
||||
|
||||
===========================================================================*/
|
||||
|
||||
@@ -350,6 +362,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
** bit 4: operator is a test (next instruction must be a jump)
|
||||
** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
|
||||
** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
|
||||
** bit 7: instruction is an MM instruction (call a metamethod)
|
||||
*/
|
||||
|
||||
LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
||||
@@ -359,6 +372,7 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
||||
#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
|
||||
#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
|
||||
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
|
||||
#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
|
||||
|
||||
/* "out top" (set top for next instruction) */
|
||||
#define isOT(i) \
|
||||
@@ -368,11 +382,11 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
||||
/* "in top" (uses top from previous instruction) */
|
||||
#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
|
||||
|
||||
#define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m))
|
||||
#define opmode(mm,ot,it,t,a,m) \
|
||||
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
|
||||
|
||||
|
||||
/* number of list items to accumulate before a SETLIST instruction */
|
||||
#define LFIELDS_PER_FLUSH 50
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
16
lopnames.h
16
lopnames.h
@@ -7,6 +7,9 @@
|
||||
#if !defined(lopnames_h)
|
||||
#define lopnames_h
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
/* ORDER OP */
|
||||
|
||||
static const char *const opnames[] = {
|
||||
@@ -15,7 +18,9 @@ static const char *const opnames[] = {
|
||||
"LOADF",
|
||||
"LOADK",
|
||||
"LOADKX",
|
||||
"LOADBOOL",
|
||||
"LOADFALSE",
|
||||
"LFALSESKIP",
|
||||
"LOADTRUE",
|
||||
"LOADNIL",
|
||||
"GETUPVAL",
|
||||
"SETUPVAL",
|
||||
@@ -30,12 +35,6 @@ static const char *const opnames[] = {
|
||||
"NEWTABLE",
|
||||
"SELF",
|
||||
"ADDI",
|
||||
"SUBI",
|
||||
"MULI",
|
||||
"MODI",
|
||||
"POWI",
|
||||
"DIVI",
|
||||
"IDIVI",
|
||||
"ADDK",
|
||||
"SUBK",
|
||||
"MULK",
|
||||
@@ -60,6 +59,9 @@ static const char *const opnames[] = {
|
||||
"BXOR",
|
||||
"SHL",
|
||||
"SHR",
|
||||
"MMBIN",
|
||||
"MMBINI",
|
||||
"MMBINK",
|
||||
"UNM",
|
||||
"BNOT",
|
||||
"NOT",
|
||||
|
||||
84
loslib.c
84
loslib.c
@@ -10,6 +10,7 @@
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -58,18 +59,20 @@
|
||||
** ===================================================================
|
||||
*/
|
||||
|
||||
#if !defined(l_time_t) /* { */
|
||||
/*
|
||||
** type to represent time_t in Lua
|
||||
*/
|
||||
#if !defined(LUA_NUMTIME) /* { */
|
||||
|
||||
#define l_timet lua_Integer
|
||||
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
|
||||
#define l_gettime(L,arg) luaL_checkinteger(L, arg)
|
||||
|
||||
static time_t l_checktime (lua_State *L, int arg) {
|
||||
lua_Integer t = luaL_checkinteger(L, arg);
|
||||
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
||||
return (time_t)t;
|
||||
}
|
||||
#else /* }{ */
|
||||
|
||||
#define l_timet lua_Number
|
||||
#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t))
|
||||
#define l_gettime(L,arg) luaL_checknumber(L, arg)
|
||||
|
||||
#endif /* } */
|
||||
|
||||
@@ -89,7 +92,7 @@ static time_t l_checktime (lua_State *L, int arg) {
|
||||
|
||||
/* 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))
|
||||
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
||||
|
||||
#endif /* } */
|
||||
|
||||
@@ -136,10 +139,11 @@ static time_t l_checktime (lua_State *L, int arg) {
|
||||
|
||||
|
||||
|
||||
|
||||
static int os_execute (lua_State *L) {
|
||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||
int stat = system(cmd);
|
||||
int stat;
|
||||
errno = 0;
|
||||
stat = system(cmd);
|
||||
if (cmd != NULL)
|
||||
return luaL_execresult(L, stat);
|
||||
else {
|
||||
@@ -193,11 +197,25 @@ static int os_clock (lua_State *L) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void setfield (lua_State *L, const char *key, int value) {
|
||||
lua_pushinteger(L, value);
|
||||
/*
|
||||
** About the overflow check: an overflow cannot occur when time
|
||||
** is represented by a lua_Integer, because either lua_Integer is
|
||||
** large enough to represent all int fields or it is not large enough
|
||||
** to represent a time that cause a field to overflow. However, if
|
||||
** times are represented as doubles and lua_Integer is int, then the
|
||||
** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
|
||||
** to compute the year.
|
||||
*/
|
||||
static void setfield (lua_State *L, const char *key, int value, int delta) {
|
||||
#if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
|
||||
if (value > LUA_MAXINTEGER - delta)
|
||||
luaL_error(L, "field '%s' is out-of-bound", key);
|
||||
#endif
|
||||
lua_pushinteger(L, (lua_Integer)value + delta);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
|
||||
static void setboolfield (lua_State *L, const char *key, int value) {
|
||||
if (value < 0) /* undefined? */
|
||||
return; /* does not set field */
|
||||
@@ -210,14 +228,14 @@ static void setboolfield (lua_State *L, const char *key, int value) {
|
||||
** Set all fields from structure 'tm' in the table on top of the stack
|
||||
*/
|
||||
static void setallfields (lua_State *L, struct tm *stm) {
|
||||
setfield(L, "sec", stm->tm_sec);
|
||||
setfield(L, "min", stm->tm_min);
|
||||
setfield(L, "hour", stm->tm_hour);
|
||||
setfield(L, "day", stm->tm_mday);
|
||||
setfield(L, "month", stm->tm_mon + 1);
|
||||
setfield(L, "year", stm->tm_year + 1900);
|
||||
setfield(L, "wday", stm->tm_wday + 1);
|
||||
setfield(L, "yday", stm->tm_yday + 1);
|
||||
setfield(L, "year", stm->tm_year, 1900);
|
||||
setfield(L, "month", stm->tm_mon, 1);
|
||||
setfield(L, "day", stm->tm_mday, 0);
|
||||
setfield(L, "hour", stm->tm_hour, 0);
|
||||
setfield(L, "min", stm->tm_min, 0);
|
||||
setfield(L, "sec", stm->tm_sec, 0);
|
||||
setfield(L, "yday", stm->tm_yday, 1);
|
||||
setfield(L, "wday", stm->tm_wday, 1);
|
||||
setboolfield(L, "isdst", stm->tm_isdst);
|
||||
}
|
||||
|
||||
@@ -230,11 +248,6 @@ static int getboolfield (lua_State *L, const char *key) {
|
||||
}
|
||||
|
||||
|
||||
/* maximum value for date fields (to avoid arithmetic overflows with 'int') */
|
||||
#if !defined(L_MAXDATEFIELD)
|
||||
#define L_MAXDATEFIELD (INT_MAX / 2)
|
||||
#endif
|
||||
|
||||
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
||||
int isnum;
|
||||
int t = lua_getfield(L, -1, key); /* get field and its type */
|
||||
@@ -247,7 +260,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
|
||||
res = d;
|
||||
}
|
||||
else {
|
||||
if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
|
||||
/* unsigned avoids overflow when lua_Integer has 32 bits */
|
||||
if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta
|
||||
: (lua_Integer)INT_MIN + delta <= res))
|
||||
return luaL_error(L, "field '%s' is out-of-bound", key);
|
||||
res -= delta;
|
||||
}
|
||||
@@ -275,6 +290,13 @@ static const char *checkoption (lua_State *L, const char *conv,
|
||||
}
|
||||
|
||||
|
||||
static time_t l_checktime (lua_State *L, int arg) {
|
||||
l_timet t = l_gettime(L, arg);
|
||||
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
||||
return (time_t)t;
|
||||
}
|
||||
|
||||
|
||||
/* maximum size for an individual 'strftime' item */
|
||||
#define SIZETIMEFMT 250
|
||||
|
||||
@@ -293,7 +315,7 @@ static int os_date (lua_State *L) {
|
||||
stm = l_localtime(&t, &tmr);
|
||||
if (stm == NULL) /* invalid date? */
|
||||
return luaL_error(L,
|
||||
"time result cannot be represented in this installation");
|
||||
"date result cannot be represented in this installation");
|
||||
if (strcmp(s, "*t") == 0) {
|
||||
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
||||
setallfields(L, stm);
|
||||
@@ -329,12 +351,12 @@ static int os_time (lua_State *L) {
|
||||
struct tm ts;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_settop(L, 1); /* make sure table is at the top */
|
||||
ts.tm_sec = getfield(L, "sec", 0, 0);
|
||||
ts.tm_min = getfield(L, "min", 0, 0);
|
||||
ts.tm_hour = getfield(L, "hour", 12, 0);
|
||||
ts.tm_mday = getfield(L, "day", -1, 0);
|
||||
ts.tm_mon = getfield(L, "month", -1, 1);
|
||||
ts.tm_year = getfield(L, "year", -1, 1900);
|
||||
ts.tm_mon = getfield(L, "month", -1, 1);
|
||||
ts.tm_mday = getfield(L, "day", -1, 0);
|
||||
ts.tm_hour = getfield(L, "hour", 12, 0);
|
||||
ts.tm_min = getfield(L, "min", 0, 0);
|
||||
ts.tm_sec = getfield(L, "sec", 0, 0);
|
||||
ts.tm_isdst = getboolfield(L, "isdst");
|
||||
t = mktime(&ts);
|
||||
setallfields(L, &ts); /* update fields with normalized values */
|
||||
|
||||
468
lparser.c
468
lparser.c
@@ -156,20 +156,15 @@ static void init_exp (expdesc *e, expkind k, int i) {
|
||||
}
|
||||
|
||||
|
||||
static void init_var (expdesc *e, expkind k, int i) {
|
||||
static void codestring (expdesc *e, TString *s) {
|
||||
e->f = e->t = NO_JUMP;
|
||||
e->k = k;
|
||||
e->u.var.idx = i;
|
||||
}
|
||||
|
||||
|
||||
static void codestring (LexState *ls, expdesc *e, TString *s) {
|
||||
init_exp(e, VK, luaK_stringK(ls->fs, s));
|
||||
e->k = VKSTR;
|
||||
e->u.strval = s;
|
||||
}
|
||||
|
||||
|
||||
static void codename (LexState *ls, expdesc *e) {
|
||||
codestring(ls, e, str_checkname(ls));
|
||||
codestring(e, str_checkname(ls));
|
||||
}
|
||||
|
||||
|
||||
@@ -177,94 +172,134 @@ static void codename (LexState *ls, expdesc *e) {
|
||||
** Register a new local variable in the active 'Proto' (for debug
|
||||
** information).
|
||||
*/
|
||||
static int registerlocalvar (LexState *ls, TString *varname) {
|
||||
FuncState *fs = ls->fs;
|
||||
static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) {
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizelocvars;
|
||||
luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
|
||||
luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars,
|
||||
LocVar, SHRT_MAX, "local variables");
|
||||
while (oldsize < f->sizelocvars)
|
||||
f->locvars[oldsize++].varname = NULL;
|
||||
f->locvars[fs->nlocvars].varname = varname;
|
||||
f->locvars[fs->ndebugvars].varname = varname;
|
||||
f->locvars[fs->ndebugvars].startpc = fs->pc;
|
||||
luaC_objbarrier(ls->L, f, varname);
|
||||
return fs->nlocvars++;
|
||||
return fs->ndebugvars++;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create a new local variable with the given 'name'.
|
||||
** Create a new local variable with the given 'name'. Return its index
|
||||
** in the function.
|
||||
*/
|
||||
static Vardesc *new_localvar (LexState *ls, TString *name) {
|
||||
static int new_localvar (LexState *ls, TString *name) {
|
||||
lua_State *L = ls->L;
|
||||
FuncState *fs = ls->fs;
|
||||
Dyndata *dyd = ls->dyd;
|
||||
Vardesc *var;
|
||||
int reg = registerlocalvar(ls, name);
|
||||
checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
|
||||
MAXVARS, "local variables");
|
||||
luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
|
||||
dyd->actvar.size, Vardesc, MAX_INT, "local variables");
|
||||
MAXVARS, "local variables");
|
||||
luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1,
|
||||
dyd->actvar.size, Vardesc, USHRT_MAX, "local variables");
|
||||
var = &dyd->actvar.arr[dyd->actvar.n++];
|
||||
var->idx = cast(short, reg);
|
||||
var->name = name;
|
||||
var->ro = 0;
|
||||
return var;
|
||||
var->vd.kind = VDKREG; /* default */
|
||||
var->vd.name = name;
|
||||
return dyd->actvar.n - 1 - fs->firstlocal;
|
||||
}
|
||||
|
||||
#define new_localvarliteral(ls,v) \
|
||||
new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1));
|
||||
new_localvar(ls, \
|
||||
luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1));
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Return the "variable description" (Vardesc) of a given
|
||||
** variable
|
||||
** Return the "variable description" (Vardesc) of a given variable.
|
||||
** (Unless noted otherwise, all variables are referred to by their
|
||||
** compiler indices.)
|
||||
*/
|
||||
static Vardesc *getlocalvardesc (FuncState *fs, int i) {
|
||||
return &fs->ls->dyd->actvar.arr[fs->firstlocal + i];
|
||||
}
|
||||
|
||||
/*
|
||||
** Get the debug-information entry for current variable 'i'.
|
||||
*/
|
||||
static LocVar *getlocvar (FuncState *fs, int i) {
|
||||
int idx = getlocalvardesc(fs, i)->idx;
|
||||
lua_assert(idx < fs->nlocvars);
|
||||
return &fs->f->locvars[idx];
|
||||
static Vardesc *getlocalvardesc (FuncState *fs, int vidx) {
|
||||
return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the "variable description" (Vardesc) of a given
|
||||
** variable or upvalue
|
||||
** Convert 'nvar', a compiler index level, to it corresponding
|
||||
** stack index level. For that, search for the highest variable
|
||||
** below that level that is in the stack and uses its stack
|
||||
** index ('sidx').
|
||||
*/
|
||||
static Vardesc *getvardesc (FuncState *fs, expdesc *e) {
|
||||
if (e->k == VLOCAL)
|
||||
return getlocalvardesc(fs, e->u.var.idx);
|
||||
else if (e->k != VUPVAL)
|
||||
return NULL; /* not a local variable */
|
||||
else { /* upvalue: must go up all levels up to the original local */
|
||||
int idx = e->u.var.idx;
|
||||
for (;;) {
|
||||
Upvaldesc *up = &fs->f->upvalues[idx];
|
||||
fs = fs->prev; /* must look at the previous level */
|
||||
idx = up->idx; /* at this index */
|
||||
if (fs == NULL) { /* no more levels? (can happen only with _ENV) */
|
||||
lua_assert(strcmp(getstr(up->name), LUA_ENV) == 0);
|
||||
return NULL;
|
||||
}
|
||||
else if (up->instack) /* got to the original level? */
|
||||
return getlocalvardesc(fs, idx);
|
||||
/* else repeat for previous level */
|
||||
}
|
||||
static int stacklevel (FuncState *fs, int nvar) {
|
||||
while (nvar-- > 0) {
|
||||
Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */
|
||||
if (vd->vd.kind != RDKCTC) /* is in the stack? */
|
||||
return vd->vd.sidx + 1;
|
||||
}
|
||||
return 0; /* no variables in the stack */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the number of variables in the stack for function 'fs'
|
||||
*/
|
||||
int luaY_nvarstack (FuncState *fs) {
|
||||
return stacklevel(fs, fs->nactvar);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get the debug-information entry for current variable 'vidx'.
|
||||
*/
|
||||
static LocVar *localdebuginfo (FuncState *fs, int vidx) {
|
||||
Vardesc *vd = getlocalvardesc(fs, vidx);
|
||||
if (vd->vd.kind == RDKCTC)
|
||||
return NULL; /* no debug info. for constants */
|
||||
else {
|
||||
int idx = vd->vd.pidx;
|
||||
lua_assert(idx < fs->ndebugvars);
|
||||
return &fs->f->locvars[idx];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create an expression representing variable 'vidx'
|
||||
*/
|
||||
static void init_var (FuncState *fs, expdesc *e, int vidx) {
|
||||
e->f = e->t = NO_JUMP;
|
||||
e->k = VLOCAL;
|
||||
e->u.var.vidx = vidx;
|
||||
e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Raises an error if variable described by 'e' is read only
|
||||
*/
|
||||
static void check_readonly (LexState *ls, expdesc *e) {
|
||||
Vardesc *vardesc = getvardesc(ls->fs, e);
|
||||
if (vardesc && vardesc->ro) { /* is variable local and const? */
|
||||
FuncState *fs = ls->fs;
|
||||
TString *varname = NULL; /* to be set if variable is const */
|
||||
switch (e->k) {
|
||||
case VCONST: {
|
||||
varname = ls->dyd->actvar.arr[e->u.info].vd.name;
|
||||
break;
|
||||
}
|
||||
case VLOCAL: {
|
||||
Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx);
|
||||
if (vardesc->vd.kind != VDKREG) /* not a regular variable? */
|
||||
varname = vardesc->vd.name;
|
||||
break;
|
||||
}
|
||||
case VUPVAL: {
|
||||
Upvaldesc *up = &fs->f->upvalues[e->u.info];
|
||||
if (up->kind != VDKREG)
|
||||
varname = up->name;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return; /* other cases cannot be read-only */
|
||||
}
|
||||
if (varname) {
|
||||
const char *msg = luaO_pushfstring(ls->L,
|
||||
"attempt to assign to const variable '%s'", getstr(vardesc->name));
|
||||
"attempt to assign to const variable '%s'", getstr(varname));
|
||||
luaK_semerror(ls, msg); /* error */
|
||||
}
|
||||
}
|
||||
@@ -272,13 +307,16 @@ static void check_readonly (LexState *ls, expdesc *e) {
|
||||
|
||||
/*
|
||||
** Start the scope for the last 'nvars' created variables.
|
||||
** (debug info.)
|
||||
*/
|
||||
static void adjustlocalvars (LexState *ls, int nvars) {
|
||||
FuncState *fs = ls->fs;
|
||||
fs->nactvar = cast_byte(fs->nactvar + nvars);
|
||||
for (; nvars; nvars--) {
|
||||
getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc;
|
||||
int stklevel = luaY_nvarstack(fs);
|
||||
int i;
|
||||
for (i = 0; i < nvars; i++) {
|
||||
int vidx = fs->nactvar++;
|
||||
Vardesc *var = getlocalvardesc(fs, vidx);
|
||||
var->vd.sidx = stklevel++;
|
||||
var->vd.pidx = registerlocalvar(ls, fs, var->vd.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,8 +327,11 @@ static void adjustlocalvars (LexState *ls, int nvars) {
|
||||
*/
|
||||
static void removevars (FuncState *fs, int tolevel) {
|
||||
fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
|
||||
while (fs->nactvar > tolevel)
|
||||
getlocvar(fs, --fs->nactvar)->endpc = fs->pc;
|
||||
while (fs->nactvar > tolevel) {
|
||||
LocVar *var = localdebuginfo(fs, --fs->nactvar);
|
||||
if (var) /* does it have debug information? */
|
||||
var->endpc = fs->pc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -308,7 +349,7 @@ static int searchupvalue (FuncState *fs, TString *name) {
|
||||
}
|
||||
|
||||
|
||||
static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
||||
static Upvaldesc *allocupvalue (FuncState *fs) {
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizeupvalues;
|
||||
checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
|
||||
@@ -316,23 +357,47 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
||||
Upvaldesc, MAXUPVAL, "upvalues");
|
||||
while (oldsize < f->sizeupvalues)
|
||||
f->upvalues[oldsize++].name = NULL;
|
||||
f->upvalues[fs->nups].instack = (v->k == VLOCAL);
|
||||
f->upvalues[fs->nups].idx = cast_byte(v->u.var.idx);
|
||||
f->upvalues[fs->nups].name = name;
|
||||
luaC_objbarrier(fs->ls->L, f, name);
|
||||
return fs->nups++;
|
||||
return &f->upvalues[fs->nups++];
|
||||
}
|
||||
|
||||
|
||||
static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
||||
Upvaldesc *up = allocupvalue(fs);
|
||||
FuncState *prev = fs->prev;
|
||||
if (v->k == VLOCAL) {
|
||||
up->instack = 1;
|
||||
up->idx = v->u.var.sidx;
|
||||
up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind;
|
||||
lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name));
|
||||
}
|
||||
else {
|
||||
up->instack = 0;
|
||||
up->idx = cast_byte(v->u.info);
|
||||
up->kind = prev->f->upvalues[v->u.info].kind;
|
||||
lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name));
|
||||
}
|
||||
up->name = name;
|
||||
luaC_objbarrier(fs->ls->L, fs->f, name);
|
||||
return fs->nups - 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Look for an active local variable with the name 'n' in the
|
||||
** function 'fs'.
|
||||
** function 'fs'. If found, initialize 'var' with it and return
|
||||
** its expression kind; otherwise return -1.
|
||||
*/
|
||||
static int searchvar (FuncState *fs, TString *n) {
|
||||
static int searchvar (FuncState *fs, TString *n, expdesc *var) {
|
||||
int i;
|
||||
for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
|
||||
if (eqstr(n, getlocvar(fs, i)->varname))
|
||||
return i;
|
||||
Vardesc *vd = getlocalvardesc(fs, i);
|
||||
if (eqstr(n, vd->vd.name)) { /* found? */
|
||||
if (vd->vd.kind == RDKCTC) /* compile-time constant? */
|
||||
init_exp(var, VCONST, fs->firstlocal + i);
|
||||
else /* real variable */
|
||||
init_var(fs, var, i);
|
||||
return var->k;
|
||||
}
|
||||
}
|
||||
return -1; /* not found */
|
||||
}
|
||||
@@ -360,22 +425,21 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
|
||||
if (fs == NULL) /* no more levels? */
|
||||
init_exp(var, VVOID, 0); /* default is global */
|
||||
else {
|
||||
int v = searchvar(fs, n); /* look up locals at current level */
|
||||
int v = searchvar(fs, n, var); /* look up locals at current level */
|
||||
if (v >= 0) { /* found? */
|
||||
init_var(var, VLOCAL, v); /* variable is local */
|
||||
if (!base)
|
||||
markupval(fs, v); /* local will be used as an upval */
|
||||
if (v == VLOCAL && !base)
|
||||
markupval(fs, var->u.var.vidx); /* local will be used as an upval */
|
||||
}
|
||||
else { /* not found as local at current level; try upvalues */
|
||||
int idx = searchupvalue(fs, n); /* try existing upvalues */
|
||||
if (idx < 0) { /* not found? */
|
||||
singlevaraux(fs->prev, n, var, 0); /* try upper levels */
|
||||
if (var->k == VVOID) /* not found? */
|
||||
return; /* it is a global */
|
||||
/* else was LOCAL or UPVAL */
|
||||
idx = newupvalue(fs, n, var); /* will be a new upvalue */
|
||||
if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */
|
||||
idx = newupvalue(fs, n, var); /* will be a new upvalue */
|
||||
else /* it is a global or a constant */
|
||||
return; /* don't need to do anything at this level */
|
||||
}
|
||||
init_var(var, VUPVAL, idx); /* new or old upvalue */
|
||||
init_exp(var, VUPVAL, idx); /* new or old upvalue */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -393,7 +457,7 @@ static void singlevar (LexState *ls, expdesc *var) {
|
||||
expdesc key;
|
||||
singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
|
||||
lua_assert(var->k != VVOID); /* this one must exist */
|
||||
codestring(ls, &key, varname); /* key is variable name */
|
||||
codestring(&key, varname); /* key is variable name */
|
||||
luaK_indexed(fs, var, &key); /* env[varname] */
|
||||
}
|
||||
}
|
||||
@@ -438,7 +502,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
|
||||
** local variable.
|
||||
*/
|
||||
static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) {
|
||||
const char *varname = getstr(getlocvar(ls->fs, gt->nactvar)->varname);
|
||||
const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name);
|
||||
const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'";
|
||||
msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname);
|
||||
luaK_semerror(ls, msg); /* raise the error */
|
||||
@@ -541,7 +605,7 @@ static int createlabel (LexState *ls, TString *name, int line,
|
||||
ll->arr[l].nactvar = fs->bl->nactvar;
|
||||
}
|
||||
if (solvegotos(ls, &ll->arr[l])) { /* need close? */
|
||||
luaK_codeABC(fs, OP_CLOSE, fs->nactvar, 0, 0);
|
||||
luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -557,10 +621,10 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) {
|
||||
/* correct pending gotos to current block */
|
||||
for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */
|
||||
Labeldesc *gt = &gl->arr[i];
|
||||
if (gt->nactvar > bl->nactvar) { /* leaving a variable scope? */
|
||||
gt->nactvar = bl->nactvar; /* update goto level */
|
||||
/* leaving a variable scope? */
|
||||
if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar))
|
||||
gt->close |= bl->upval; /* jump may need a close */
|
||||
}
|
||||
gt->nactvar = bl->nactvar; /* update goto level */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,7 +638,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
|
||||
bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc);
|
||||
bl->previous = fs->bl;
|
||||
fs->bl = bl;
|
||||
lua_assert(fs->freereg == fs->nactvar);
|
||||
lua_assert(fs->freereg == luaY_nvarstack(fs));
|
||||
}
|
||||
|
||||
|
||||
@@ -599,14 +663,15 @@ static void leaveblock (FuncState *fs) {
|
||||
BlockCnt *bl = fs->bl;
|
||||
LexState *ls = fs->ls;
|
||||
int hasclose = 0;
|
||||
int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */
|
||||
if (bl->isloop) /* fix pending breaks? */
|
||||
hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0);
|
||||
if (!hasclose && bl->previous && bl->upval)
|
||||
luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
|
||||
luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0);
|
||||
fs->bl = bl->previous;
|
||||
removevars(fs, bl->nactvar);
|
||||
lua_assert(bl->nactvar == fs->nactvar);
|
||||
fs->freereg = fs->nactvar; /* free registers */
|
||||
fs->freereg = stklevel; /* free registers */
|
||||
ls->dyd->label.n = bl->firstlabel; /* remove local labels */
|
||||
if (bl->previous) /* inner block? */
|
||||
movegotosout(fs, bl); /* update pending gotos to outer block */
|
||||
@@ -639,9 +704,10 @@ static Proto *addprototype (LexState *ls) {
|
||||
|
||||
/*
|
||||
** codes instruction to create new closure in parent function.
|
||||
** The OP_CLOSURE instruction must use the last available register,
|
||||
** The OP_CLOSURE instruction uses the last available register,
|
||||
** so that, if it invokes the GC, the GC knows which registers
|
||||
** are in use at that time.
|
||||
|
||||
*/
|
||||
static void codeclosure (LexState *ls, expdesc *v) {
|
||||
FuncState *fs = ls->fs->prev;
|
||||
@@ -664,13 +730,14 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
|
||||
fs->nabslineinfo = 0;
|
||||
fs->np = 0;
|
||||
fs->nups = 0;
|
||||
fs->nlocvars = 0;
|
||||
fs->ndebugvars = 0;
|
||||
fs->nactvar = 0;
|
||||
fs->needclose = 0;
|
||||
fs->firstlocal = ls->dyd->actvar.n;
|
||||
fs->firstlabel = ls->dyd->label.n;
|
||||
fs->bl = NULL;
|
||||
f->source = ls->source;
|
||||
luaC_objbarrier(ls->L, f, f->source);
|
||||
f->maxstacksize = 2; /* registers 0/1 are always valid */
|
||||
enterblock(fs, bl, 0);
|
||||
}
|
||||
@@ -680,7 +747,7 @@ static void close_func (LexState *ls) {
|
||||
lua_State *L = ls->L;
|
||||
FuncState *fs = ls->fs;
|
||||
Proto *f = fs->f;
|
||||
luaK_ret(fs, fs->nactvar, 0); /* final return */
|
||||
luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */
|
||||
leaveblock(fs);
|
||||
lua_assert(fs->bl == NULL);
|
||||
luaK_finish(fs);
|
||||
@@ -690,7 +757,7 @@ static void close_func (LexState *ls) {
|
||||
fs->nabslineinfo, AbsLineInfo);
|
||||
luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue);
|
||||
luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *);
|
||||
luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
|
||||
luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar);
|
||||
luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
|
||||
ls->fs = fs->prev;
|
||||
luaC_checkGC(L);
|
||||
@@ -758,16 +825,16 @@ static void yindex (LexState *ls, expdesc *v) {
|
||||
*/
|
||||
|
||||
|
||||
struct ConsControl {
|
||||
typedef struct ConsControl {
|
||||
expdesc v; /* last list item read */
|
||||
expdesc *t; /* table descriptor */
|
||||
int nh; /* total number of 'record' elements */
|
||||
int na; /* total number of array elements */
|
||||
int na; /* number of array elements already stored */
|
||||
int tostore; /* number of array elements pending to be stored */
|
||||
};
|
||||
} ConsControl;
|
||||
|
||||
|
||||
static void recfield (LexState *ls, struct ConsControl *cc) {
|
||||
static void recfield (LexState *ls, ConsControl *cc) {
|
||||
/* recfield -> (NAME | '['exp']') = exp */
|
||||
FuncState *fs = ls->fs;
|
||||
int reg = ls->fs->freereg;
|
||||
@@ -788,18 +855,19 @@ static void recfield (LexState *ls, struct ConsControl *cc) {
|
||||
}
|
||||
|
||||
|
||||
static void closelistfield (FuncState *fs, struct ConsControl *cc) {
|
||||
static void closelistfield (FuncState *fs, ConsControl *cc) {
|
||||
if (cc->v.k == VVOID) return; /* there is no list item */
|
||||
luaK_exp2nextreg(fs, &cc->v);
|
||||
cc->v.k = VVOID;
|
||||
if (cc->tostore == LFIELDS_PER_FLUSH) {
|
||||
luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */
|
||||
cc->na += cc->tostore;
|
||||
cc->tostore = 0; /* no more items pending */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
|
||||
static void lastlistfield (FuncState *fs, ConsControl *cc) {
|
||||
if (cc->tostore == 0) return;
|
||||
if (hasmultret(cc->v.k)) {
|
||||
luaK_setmultret(fs, &cc->v);
|
||||
@@ -811,19 +879,18 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
|
||||
luaK_exp2nextreg(fs, &cc->v);
|
||||
luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore);
|
||||
}
|
||||
cc->na += cc->tostore;
|
||||
}
|
||||
|
||||
|
||||
static void listfield (LexState *ls, struct ConsControl *cc) {
|
||||
static void listfield (LexState *ls, ConsControl *cc) {
|
||||
/* listfield -> exp */
|
||||
expr(ls, &cc->v);
|
||||
checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
|
||||
cc->na++;
|
||||
cc->tostore++;
|
||||
}
|
||||
|
||||
|
||||
static void field (LexState *ls, struct ConsControl *cc) {
|
||||
static void field (LexState *ls, ConsControl *cc) {
|
||||
/* field -> listfield | recfield */
|
||||
switch(ls->t.token) {
|
||||
case TK_NAME: { /* may be 'listfield' or 'recfield' */
|
||||
@@ -851,12 +918,13 @@ static void constructor (LexState *ls, expdesc *t) {
|
||||
FuncState *fs = ls->fs;
|
||||
int line = ls->linenumber;
|
||||
int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
|
||||
struct ConsControl cc;
|
||||
ConsControl cc;
|
||||
luaK_code(fs, 0); /* space for extra arg. */
|
||||
cc.na = cc.nh = cc.tostore = 0;
|
||||
cc.t = t;
|
||||
init_exp(t, VRELOC, pc);
|
||||
init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */
|
||||
luaK_reserveregs(fs, 1);
|
||||
init_exp(&cc.v, VVOID, 0); /* no value (yet) */
|
||||
luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */
|
||||
checknext(ls, '{');
|
||||
do {
|
||||
lua_assert(cc.v.k == VVOID || cc.tostore > 0);
|
||||
@@ -866,8 +934,7 @@ static void constructor (LexState *ls, expdesc *t) {
|
||||
} while (testnext(ls, ',') || testnext(ls, ';'));
|
||||
check_match(ls, '}', '{', line);
|
||||
lastlistfield(fs, &cc);
|
||||
SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
|
||||
SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */
|
||||
luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh);
|
||||
}
|
||||
|
||||
/* }====================================================================== */
|
||||
@@ -956,7 +1023,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
||||
args.k = VVOID;
|
||||
else {
|
||||
explist(ls, &args);
|
||||
luaK_setmultret(fs, &args);
|
||||
if (hasmultret(args.k))
|
||||
luaK_setmultret(fs, &args);
|
||||
}
|
||||
check_match(ls, ')', '(', line);
|
||||
break;
|
||||
@@ -966,7 +1034,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) {
|
||||
break;
|
||||
}
|
||||
case TK_STRING: { /* funcargs -> STRING */
|
||||
codestring(ls, &args, ls->t.seminfo.ts);
|
||||
codestring(&args, ls->t.seminfo.ts);
|
||||
luaX_next(ls); /* must use 'seminfo' before 'next' */
|
||||
break;
|
||||
}
|
||||
@@ -1074,7 +1142,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
|
||||
break;
|
||||
}
|
||||
case TK_STRING: {
|
||||
codestring(ls, v, ls->t.seminfo.ts);
|
||||
codestring(v, ls->t.seminfo.ts);
|
||||
break;
|
||||
}
|
||||
case TK_NIL: {
|
||||
@@ -1257,20 +1325,20 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
|
||||
for (; lh; lh = lh->prev) { /* check all previous assignments */
|
||||
if (vkisindexed(lh->v.k)) { /* assignment to table field? */
|
||||
if (lh->v.k == VINDEXUP) { /* is table an upvalue? */
|
||||
if (v->k == VUPVAL && lh->v.u.ind.t == v->u.var.idx) {
|
||||
if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) {
|
||||
conflict = 1; /* table is the upvalue being assigned now */
|
||||
lh->v.k = VINDEXSTR;
|
||||
lh->v.u.ind.t = extra; /* assignment will use safe copy */
|
||||
}
|
||||
}
|
||||
else { /* table is a register */
|
||||
if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.idx) {
|
||||
if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) {
|
||||
conflict = 1; /* table is the local being assigned now */
|
||||
lh->v.u.ind.t = extra; /* assignment will use safe copy */
|
||||
}
|
||||
/* is index the local being assigned? */
|
||||
if (lh->v.k == VINDEXED && v->k == VLOCAL &&
|
||||
lh->v.u.ind.idx == v->u.var.idx) {
|
||||
lh->v.u.ind.idx == v->u.var.sidx) {
|
||||
conflict = 1;
|
||||
lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
|
||||
}
|
||||
@@ -1279,14 +1347,16 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
|
||||
}
|
||||
if (conflict) {
|
||||
/* copy upvalue/local value to a temporary (in position 'extra') */
|
||||
OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
|
||||
luaK_codeABC(fs, op, extra, v->u.var.idx, 0);
|
||||
if (v->k == VLOCAL)
|
||||
luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0);
|
||||
else
|
||||
luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0);
|
||||
luaK_reserveregs(fs, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Parse and compile a mulitple assignment. The first "variable"
|
||||
** Parse and compile a multiple assignment. The first "variable"
|
||||
** (a 'suffixedexp') was already read by the caller.
|
||||
**
|
||||
** assignment -> suffixedexp restassign
|
||||
@@ -1343,8 +1413,9 @@ static void gotostat (LexState *ls) {
|
||||
newgotoentry(ls, name, line, luaK_jump(fs));
|
||||
else { /* found a label */
|
||||
/* backward jump; will be resolved here */
|
||||
if (fs->nactvar > lb->nactvar) /* leaving the scope of some variable? */
|
||||
luaK_codeABC(fs, OP_CLOSE, lb->nactvar, 0, 0);
|
||||
int lblevel = stacklevel(fs, lb->nactvar); /* label level */
|
||||
if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */
|
||||
luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0);
|
||||
/* create jump and link it to the label */
|
||||
luaK_patchlist(fs, luaK_jump(fs), lb->pc);
|
||||
}
|
||||
@@ -1419,7 +1490,7 @@ static void repeatstat (LexState *ls, int line) {
|
||||
if (bl2.upval) { /* upvalues? */
|
||||
int exit = luaK_jump(fs); /* normal exit must jump over fix */
|
||||
luaK_patchtohere(fs, condexit); /* repetition must close upvalues */
|
||||
luaK_codeABC(fs, OP_CLOSE, bl2.nactvar, 0, 0);
|
||||
luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0);
|
||||
condexit = luaK_jump(fs); /* repeat after closing upvalues */
|
||||
luaK_patchtohere(fs, exit); /* normal exit comes to here */
|
||||
}
|
||||
@@ -1462,8 +1533,8 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) {
|
||||
*/
|
||||
static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
|
||||
/* forbody -> DO block */
|
||||
static OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP};
|
||||
static OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP};
|
||||
static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP};
|
||||
static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP};
|
||||
BlockCnt bl;
|
||||
FuncState *fs = ls->fs;
|
||||
int prep, endfor;
|
||||
@@ -1478,7 +1549,6 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
|
||||
if (isgen) { /* generic for? */
|
||||
luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
|
||||
luaK_fixline(fs, line);
|
||||
base += 2; /* base for 'OP_TFORLOOP' (skips function and state) */
|
||||
}
|
||||
endfor = luaK_codeABx(fs, forloop[isgen], base, 0);
|
||||
fixforjump(fs, endfor, prep + 1, 1);
|
||||
@@ -1490,9 +1560,9 @@ static void fornum (LexState *ls, TString *varname, int line) {
|
||||
/* fornum -> NAME = exp,exp[,exp] forbody */
|
||||
FuncState *fs = ls->fs;
|
||||
int base = fs->freereg;
|
||||
new_localvarliteral(ls, "(for index)");
|
||||
new_localvarliteral(ls, "(for limit)");
|
||||
new_localvarliteral(ls, "(for step)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
new_localvar(ls, varname);
|
||||
checknext(ls, '=');
|
||||
exp1(ls); /* initial value */
|
||||
@@ -1517,11 +1587,10 @@ static void forlist (LexState *ls, TString *indexname) {
|
||||
int line;
|
||||
int base = fs->freereg;
|
||||
/* create control variables */
|
||||
new_localvarliteral(ls, "(for generator)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
markupval(fs, fs->nactvar); /* state may create an upvalue */
|
||||
new_localvarliteral(ls, "(for control)");
|
||||
new_localvarliteral(ls, "(for toclose)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
new_localvarliteral(ls, "(for state)");
|
||||
/* create declared variables */
|
||||
new_localvar(ls, indexname);
|
||||
while (testnext(ls, ',')) {
|
||||
@@ -1532,6 +1601,7 @@ static void forlist (LexState *ls, TString *indexname) {
|
||||
line = ls->linenumber;
|
||||
adjust_assign(ls, 4, explist(ls, &e), &e);
|
||||
adjustlocalvars(ls, 4); /* control variables */
|
||||
markupval(fs, fs->nactvar); /* last control var. must be closed */
|
||||
luaK_checkstack(fs, 3); /* extra space to call generator */
|
||||
forbody(ls, base, line, nvars - 4, 1);
|
||||
}
|
||||
@@ -1574,7 +1644,8 @@ static int issinglejump (LexState *ls, TString **label, int *target) {
|
||||
TString *lname = ls->lookahead.seminfo.ts; /* label's id */
|
||||
Labeldesc *lb = findlabel(ls, lname);
|
||||
if (lb) { /* a backward jump? */
|
||||
if (ls->fs->nactvar > lb->nactvar) /* needs to close variables? */
|
||||
/* does it need to close variables? */
|
||||
if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar))
|
||||
return 0; /* not a single jump; cannot optimize */
|
||||
*target = lb->pc;
|
||||
}
|
||||
@@ -1646,21 +1717,60 @@ static void ifstat (LexState *ls, int line) {
|
||||
static void localfunc (LexState *ls) {
|
||||
expdesc b;
|
||||
FuncState *fs = ls->fs;
|
||||
int fvar = fs->nactvar; /* function's variable index */
|
||||
new_localvar(ls, str_checkname(ls)); /* new local variable */
|
||||
adjustlocalvars(ls, 1); /* enter its scope */
|
||||
body(ls, &b, 0, ls->linenumber); /* function created in next register */
|
||||
/* debug information will only see the variable after this point! */
|
||||
getlocvar(fs, b.u.info)->startpc = fs->pc;
|
||||
localdebuginfo(fs, fvar)->startpc = fs->pc;
|
||||
}
|
||||
|
||||
|
||||
static void commonlocalstat (LexState *ls) {
|
||||
/* stat -> LOCAL NAME {',' NAME} ['=' explist] */
|
||||
static int getlocalattribute (LexState *ls) {
|
||||
/* ATTRIB -> ['<' Name '>'] */
|
||||
if (testnext(ls, '<')) {
|
||||
const char *attr = getstr(str_checkname(ls));
|
||||
checknext(ls, '>');
|
||||
if (strcmp(attr, "const") == 0)
|
||||
return RDKCONST; /* read-only variable */
|
||||
else if (strcmp(attr, "close") == 0)
|
||||
return RDKTOCLOSE; /* to-be-closed variable */
|
||||
else
|
||||
luaK_semerror(ls,
|
||||
luaO_pushfstring(ls->L, "unknown attribute '%s'", attr));
|
||||
}
|
||||
return VDKREG; /* regular variable */
|
||||
}
|
||||
|
||||
|
||||
static void checktoclose (LexState *ls, int level) {
|
||||
if (level != -1) { /* is there a to-be-closed variable? */
|
||||
FuncState *fs = ls->fs;
|
||||
markupval(fs, level + 1);
|
||||
fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */
|
||||
luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void localstat (LexState *ls) {
|
||||
/* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */
|
||||
FuncState *fs = ls->fs;
|
||||
int toclose = -1; /* index of to-be-closed variable (if any) */
|
||||
Vardesc *var; /* last variable */
|
||||
int vidx, kind; /* index and kind of last variable */
|
||||
int nvars = 0;
|
||||
int nexps;
|
||||
expdesc e;
|
||||
do {
|
||||
new_localvar(ls, str_checkname(ls));
|
||||
vidx = new_localvar(ls, str_checkname(ls));
|
||||
kind = getlocalattribute(ls);
|
||||
getlocalvardesc(fs, vidx)->vd.kind = kind;
|
||||
if (kind == RDKTOCLOSE) { /* to-be-closed? */
|
||||
if (toclose != -1) /* one already present? */
|
||||
luaK_semerror(ls, "multiple to-be-closed variables in local list");
|
||||
toclose = fs->nactvar + nvars;
|
||||
}
|
||||
nvars++;
|
||||
} while (testnext(ls, ','));
|
||||
if (testnext(ls, '='))
|
||||
@@ -1669,45 +1779,19 @@ static void commonlocalstat (LexState *ls) {
|
||||
e.k = VVOID;
|
||||
nexps = 0;
|
||||
}
|
||||
adjust_assign(ls, nvars, nexps, &e);
|
||||
adjustlocalvars(ls, nvars);
|
||||
}
|
||||
|
||||
|
||||
static void tocloselocalstat (LexState *ls, Vardesc *var) {
|
||||
FuncState *fs = ls->fs;
|
||||
var->ro = 1; /* to-be-closed variables are always read-only */
|
||||
markupval(fs, fs->nactvar);
|
||||
fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */
|
||||
luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
static void attriblocalstat (LexState *ls) {
|
||||
Vardesc *var;
|
||||
TString *attr = str_checkname(ls);
|
||||
testnext(ls, '>');
|
||||
var = new_localvar(ls, str_checkname(ls));
|
||||
checknext(ls, '=');
|
||||
exp1(ls);
|
||||
adjustlocalvars(ls, 1);
|
||||
if (strcmp(getstr(attr), "const") == 0)
|
||||
var->ro = 1; /* set variable as read-only */
|
||||
else if (strcmp(getstr(attr), "toclose") == 0)
|
||||
tocloselocalstat(ls, var);
|
||||
else
|
||||
luaK_semerror(ls,
|
||||
luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr)));
|
||||
}
|
||||
|
||||
|
||||
static void localstat (LexState *ls) {
|
||||
/* stat -> LOCAL NAME {',' NAME} ['=' explist]
|
||||
| LOCAL *toclose NAME '=' exp */
|
||||
if (testnext(ls, '<'))
|
||||
attriblocalstat(ls);
|
||||
else
|
||||
commonlocalstat(ls);
|
||||
var = getlocalvardesc(fs, vidx); /* get last variable */
|
||||
if (nvars == nexps && /* no adjustments? */
|
||||
var->vd.kind == RDKCONST && /* last variable is const? */
|
||||
luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */
|
||||
var->vd.kind = RDKCTC; /* variable is a compile-time constant */
|
||||
adjustlocalvars(ls, nvars - 1); /* exclude last variable */
|
||||
fs->nactvar++; /* but count it */
|
||||
}
|
||||
else {
|
||||
adjust_assign(ls, nvars, nexps, &e);
|
||||
adjustlocalvars(ls, nvars);
|
||||
}
|
||||
checktoclose(ls, toclose);
|
||||
}
|
||||
|
||||
|
||||
@@ -1747,8 +1831,9 @@ static void exprstat (LexState *ls) {
|
||||
restassign(ls, &v, 1);
|
||||
}
|
||||
else { /* stat -> func */
|
||||
Instruction *inst = &getinstruction(fs, &v.v);
|
||||
Instruction *inst;
|
||||
check_condition(ls, v.v.k == VCALL, "syntax error");
|
||||
inst = &getinstruction(fs, &v.v);
|
||||
SETARG_C(*inst, 1); /* call statement uses no results */
|
||||
}
|
||||
}
|
||||
@@ -1759,7 +1844,7 @@ static void retstat (LexState *ls) {
|
||||
FuncState *fs = ls->fs;
|
||||
expdesc e;
|
||||
int nret; /* number of values being returned */
|
||||
int first = fs->nactvar; /* first slot to be returned */
|
||||
int first = luaY_nvarstack(fs); /* first slot to be returned */
|
||||
if (block_follow(ls, 1) || ls->t.token == ';')
|
||||
nret = 0; /* return no values */
|
||||
else {
|
||||
@@ -1768,7 +1853,7 @@ static void retstat (LexState *ls) {
|
||||
luaK_setmultret(fs, &e);
|
||||
if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */
|
||||
SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
|
||||
lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar);
|
||||
lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs));
|
||||
}
|
||||
nret = LUA_MULTRET; /* return all values */
|
||||
}
|
||||
@@ -1853,8 +1938,8 @@ static void statement (LexState *ls) {
|
||||
}
|
||||
}
|
||||
lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
|
||||
ls->fs->freereg >= ls->fs->nactvar);
|
||||
ls->fs->freereg = ls->fs->nactvar; /* free registers */
|
||||
ls->fs->freereg >= luaY_nvarstack(ls->fs));
|
||||
ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */
|
||||
leavelevel(ls);
|
||||
}
|
||||
|
||||
@@ -1867,11 +1952,15 @@ static void statement (LexState *ls) {
|
||||
*/
|
||||
static void mainfunc (LexState *ls, FuncState *fs) {
|
||||
BlockCnt bl;
|
||||
expdesc v;
|
||||
Upvaldesc *env;
|
||||
open_func(ls, fs, &bl);
|
||||
setvararg(fs, 0); /* main function is always declared vararg */
|
||||
init_var(&v, VLOCAL, 0); /* create and... */
|
||||
newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
|
||||
env = allocupvalue(fs); /* ...set environment upvalue */
|
||||
env->instack = 1;
|
||||
env->idx = 0;
|
||||
env->kind = VDKREG;
|
||||
env->name = ls->envn;
|
||||
luaC_objbarrier(ls->L, fs->f, env->name);
|
||||
luaX_next(ls); /* read first token */
|
||||
statlist(ls); /* parse main body */
|
||||
check(ls, TK_EOS);
|
||||
@@ -1890,6 +1979,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
sethvalue2s(L, L->top, lexstate.h); /* anchor it */
|
||||
luaD_inctop(L);
|
||||
funcstate.f = cl->p = luaF_newproto(L);
|
||||
luaC_objbarrier(L, cl, cl->p);
|
||||
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
|
||||
luaC_objbarrier(L, funcstate.f, funcstate.f->source);
|
||||
lexstate.buff = buff;
|
||||
|
||||
43
lparser.h
43
lparser.h
@@ -30,11 +30,15 @@ typedef enum {
|
||||
VFALSE, /* constant false */
|
||||
VK, /* constant in 'k'; info = index of constant in 'k' */
|
||||
VKFLT, /* floating constant; nval = numerical float value */
|
||||
VKINT, /* integer constant; nval = numerical integer value */
|
||||
VKINT, /* integer constant; ival = numerical integer value */
|
||||
VKSTR, /* string constant; strval = TString address;
|
||||
(string is fixed by the lexer) */
|
||||
VNONRELOC, /* expression has its value in a fixed register;
|
||||
info = result register */
|
||||
VLOCAL, /* local variable; var.idx = local register */
|
||||
VUPVAL, /* upvalue variable; var.idx = index of upvalue in 'upvalues' */
|
||||
VLOCAL, /* local variable; var.sidx = stack index (local register);
|
||||
var.vidx = relative index in 'actvar.arr' */
|
||||
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
|
||||
VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */
|
||||
VINDEXED, /* indexed variable;
|
||||
ind.t = table register;
|
||||
ind.idx = key's R index */
|
||||
@@ -65,13 +69,15 @@ typedef struct expdesc {
|
||||
union {
|
||||
lua_Integer ival; /* for VKINT */
|
||||
lua_Number nval; /* for VKFLT */
|
||||
TString *strval; /* for VKSTR */
|
||||
int info; /* for generic use */
|
||||
struct { /* for indexed variables */
|
||||
short idx; /* index (R or "long" K) */
|
||||
lu_byte t; /* table (register or upvalue) */
|
||||
} ind;
|
||||
struct { /* for local variables and upvalues */
|
||||
lu_byte idx; /* index of the variable */
|
||||
struct { /* for local variables */
|
||||
lu_byte sidx; /* index in the stack */
|
||||
unsigned short vidx; /* compiler index (in 'actvar.arr') */
|
||||
} var;
|
||||
} u;
|
||||
int t; /* patch list of 'exit when true' */
|
||||
@@ -79,20 +85,32 @@ typedef struct expdesc {
|
||||
} expdesc;
|
||||
|
||||
|
||||
/* kinds of variables */
|
||||
#define VDKREG 0 /* regular */
|
||||
#define RDKCONST 1 /* constant */
|
||||
#define RDKTOCLOSE 2 /* to-be-closed */
|
||||
#define RDKCTC 3 /* compile-time constant */
|
||||
|
||||
/* description of an active local variable */
|
||||
typedef struct Vardesc {
|
||||
TString *name;
|
||||
short idx; /* index of the variable in the Proto's 'locvars' array */
|
||||
lu_byte ro; /* true if variable is 'const' */
|
||||
typedef union Vardesc {
|
||||
struct {
|
||||
TValuefields; /* constant value (if it is a compile-time constant) */
|
||||
lu_byte kind;
|
||||
lu_byte sidx; /* index of the variable in the stack */
|
||||
short pidx; /* index of the variable in the Proto's 'locvars' array */
|
||||
TString *name; /* variable name */
|
||||
} vd;
|
||||
TValue k; /* constant value (if any) */
|
||||
} Vardesc;
|
||||
|
||||
|
||||
|
||||
/* description of pending goto statements and label statements */
|
||||
typedef struct Labeldesc {
|
||||
TString *name; /* label identifier */
|
||||
int pc; /* position in code */
|
||||
int line; /* line where it appeared */
|
||||
lu_byte nactvar; /* local level where it appears in current block */
|
||||
lu_byte nactvar; /* number of active variables in that position */
|
||||
lu_byte close; /* goto that escapes upvalues */
|
||||
} Labeldesc;
|
||||
|
||||
@@ -107,7 +125,7 @@ typedef struct Labellist {
|
||||
|
||||
/* dynamic structures used by the parser */
|
||||
typedef struct Dyndata {
|
||||
struct { /* list of active local variables */
|
||||
struct { /* list of all active local variables */
|
||||
Vardesc *arr;
|
||||
int n;
|
||||
int size;
|
||||
@@ -135,7 +153,7 @@ typedef struct FuncState {
|
||||
int nabslineinfo; /* number of elements in 'abslineinfo' */
|
||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||
int firstlabel; /* index of first label (in 'dyd->label->arr') */
|
||||
short nlocvars; /* number of elements in 'f->locvars' */
|
||||
short ndebugvars; /* number of elements in 'f->locvars' */
|
||||
lu_byte nactvar; /* number of active local variables */
|
||||
lu_byte nups; /* number of upvalues */
|
||||
lu_byte freereg; /* first free register */
|
||||
@@ -144,6 +162,7 @@ typedef struct FuncState {
|
||||
} FuncState;
|
||||
|
||||
|
||||
LUAI_FUNC int luaY_nvarstack (FuncState *fs);
|
||||
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar);
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
/*
|
||||
** Windows stuff
|
||||
*/
|
||||
#if defined(_WIN32) /* { */
|
||||
#if defined(_WIN32) /* { */
|
||||
|
||||
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
||||
|
||||
131
lstate.c
131
lstate.c
@@ -76,7 +76,7 @@ static unsigned int luai_makeseed (lua_State *L) {
|
||||
addbuff(buff, p, &h); /* local variable */
|
||||
addbuff(buff, p, &lua_newstate); /* public function */
|
||||
lua_assert(p == sizeof(buff));
|
||||
return luaS_hash(buff, p, h);
|
||||
return luaS_hash(buff, p, h, 1);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -96,36 +96,58 @@ void luaE_setdebt (global_State *g, l_mem debt) {
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
|
||||
global_State *g = G(L);
|
||||
int ccalls;
|
||||
luaE_freeCI(L); /* release unused CIs */
|
||||
ccalls = getCcalls(L);
|
||||
if (limit >= 40000)
|
||||
return 0; /* out of bounds */
|
||||
limit += CSTACKERR;
|
||||
if (L != g-> mainthread)
|
||||
return 0; /* only main thread can change the C stack */
|
||||
else if (ccalls <= CSTACKERR)
|
||||
return 0; /* handling overflow */
|
||||
else {
|
||||
int diff = limit - g->Cstacklimit;
|
||||
if (ccalls + diff <= CSTACKERR)
|
||||
return 0; /* new limit would cause an overflow */
|
||||
g->Cstacklimit = limit; /* set new limit */
|
||||
L->nCcalls += diff; /* correct 'nCcalls' */
|
||||
return limit - diff - CSTACKERR; /* success; return previous limit */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Increment count of "C calls" and check for overflows. In case of
|
||||
** Decrement count of "C calls" and check for overflows. In case of
|
||||
** a stack overflow, check appropriate error ("regular" overflow or
|
||||
** overflow while handling stack overflow).
|
||||
** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than
|
||||
** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means
|
||||
** it has just entered the "overflow zone", so the function raises an
|
||||
** overflow error.
|
||||
** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2
|
||||
** (which means it is already handling an overflow) but smaller than
|
||||
** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message
|
||||
** handling to work).
|
||||
** Otherwise, report a stack overflow while handling a stack overflow
|
||||
** (probably caused by a repeating error in the message handling
|
||||
** function).
|
||||
** overflow while handling stack overflow). If 'nCcalls' is smaller
|
||||
** than CSTACKERR but larger than CSTACKMARK, it means it has just
|
||||
** entered the "overflow zone", so the function raises an overflow
|
||||
** error. If 'nCcalls' is smaller than CSTACKMARK (which means it is
|
||||
** already handling an overflow) but larger than CSTACKERRMARK, does
|
||||
** not report an error (to allow message handling to work). Otherwise,
|
||||
** report a stack overflow while handling a stack overflow (probably
|
||||
** caused by a repeating error in the message handling function).
|
||||
*/
|
||||
|
||||
void luaE_enterCcall (lua_State *L) {
|
||||
int ncalls = getCcalls(L);
|
||||
L->nCcalls++;
|
||||
if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */
|
||||
L->nCcalls--;
|
||||
if (ncalls <= CSTACKERR) { /* possible overflow? */
|
||||
luaE_freeCI(L); /* release unused CIs */
|
||||
ncalls = getCcalls(L); /* update call count */
|
||||
if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */
|
||||
if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) {
|
||||
/* no error before increments; raise the error now */
|
||||
L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */
|
||||
if (ncalls <= CSTACKERR) { /* still overflow? */
|
||||
if (ncalls <= CSTACKERRMARK) /* below error-handling zone? */
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
|
||||
else if (ncalls >= CSTACKMARK) {
|
||||
/* not in error-handling zone; raise the error now */
|
||||
L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */
|
||||
luaG_runerror(L, "C stack overflow");
|
||||
}
|
||||
else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3)))
|
||||
luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
|
||||
/* else stack is in the error-handling zone;
|
||||
allow message handler to work */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,32 +175,39 @@ void luaE_freeCI (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
CallInfo *next = ci->next;
|
||||
ci->next = NULL;
|
||||
L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */
|
||||
L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */
|
||||
while ((ci = next) != NULL) {
|
||||
next = ci->next;
|
||||
luaM_free(L, ci);
|
||||
L->nci--;
|
||||
}
|
||||
L->nCcalls += L->nci; /* adjust result */
|
||||
L->nCcalls -= L->nci; /* adjust result */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** free half of the CallInfo structures not in use by a thread
|
||||
** free half of the CallInfo structures not in use by a thread,
|
||||
** keeping the first one.
|
||||
*/
|
||||
void luaE_shrinkCI (lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
CallInfo *next2; /* next's next */
|
||||
L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */
|
||||
/* while there are two nexts */
|
||||
while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
|
||||
luaM_free(L, ci->next); /* free next */
|
||||
CallInfo *ci = L->ci->next; /* first free CallInfo */
|
||||
CallInfo *next;
|
||||
if (ci == NULL)
|
||||
return; /* no extra elements */
|
||||
L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */
|
||||
while ((next = ci->next) != NULL) { /* two extra elements? */
|
||||
CallInfo *next2 = next->next; /* next's next */
|
||||
ci->next = next2; /* remove next from the list */
|
||||
L->nci--;
|
||||
ci->next = next2; /* remove 'next' from the list */
|
||||
next2->previous = ci;
|
||||
ci = next2; /* keep next's next */
|
||||
luaM_free(L, next); /* free next */
|
||||
if (next2 == NULL)
|
||||
break; /* no more elements */
|
||||
else {
|
||||
next2->previous = ci;
|
||||
ci = next2; /* continue */
|
||||
}
|
||||
}
|
||||
L->nCcalls += L->nci; /* adjust result */
|
||||
L->nCcalls -= L->nci; /* adjust result */
|
||||
}
|
||||
|
||||
|
||||
@@ -264,7 +293,6 @@ static void preinit_thread (lua_State *L, global_State *g) {
|
||||
L->stacksize = 0;
|
||||
L->twups = L; /* thread has no upvalues */
|
||||
L->errorJmp = NULL;
|
||||
L->nCcalls = 0;
|
||||
L->hook = NULL;
|
||||
L->hookmask = 0;
|
||||
L->basehookcount = 0;
|
||||
@@ -297,7 +325,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
|
||||
/* create new thread */
|
||||
L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
|
||||
L1->marked = luaC_white(g);
|
||||
L1->tt = LUA_TTHREAD;
|
||||
L1->tt = LUA_VTHREAD;
|
||||
/* link it on list 'allgc' */
|
||||
L1->next = g->allgc;
|
||||
g->allgc = obj2gco(L1);
|
||||
@@ -305,6 +333,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
|
||||
setthvalue2s(L, L->top, L1);
|
||||
api_incr_top(L);
|
||||
preinit_thread(L1, g);
|
||||
L1->nCcalls = getCcalls(L);
|
||||
L1->hookmask = L->hookmask;
|
||||
L1->basehookcount = L->basehookcount;
|
||||
L1->hook = L->hook;
|
||||
@@ -333,19 +362,18 @@ int lua_resetthread (lua_State *L) {
|
||||
CallInfo *ci;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
ci = &L->base_ci;
|
||||
status = luaF_close(L, L->stack, CLOSEPROTECT);
|
||||
L->ci = ci = &L->base_ci; /* unwind CallInfo list */
|
||||
setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */
|
||||
ci->func = L->stack;
|
||||
ci->callstatus = CIST_C;
|
||||
status = luaF_close(L, L->stack, CLOSEPROTECT);
|
||||
if (status != CLOSEPROTECT) /* real errors? */
|
||||
luaD_seterrorobj(L, status, L->stack + 1);
|
||||
else {
|
||||
status = LUA_OK;
|
||||
L->top = L->stack + 1;
|
||||
}
|
||||
ci->callstatus = CIST_C;
|
||||
ci->func = L->stack;
|
||||
ci->top = L->top + LUA_MINSTACK;
|
||||
L->ci = ci;
|
||||
L->status = status;
|
||||
lua_unlock(L);
|
||||
return status;
|
||||
@@ -360,12 +388,13 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
if (l == NULL) return NULL;
|
||||
L = &l->l.l;
|
||||
g = &l->g;
|
||||
L->tt = LUA_TTHREAD;
|
||||
L->tt = LUA_VTHREAD;
|
||||
g->currentwhite = bitmask(WHITE0BIT);
|
||||
L->marked = luaC_white(g);
|
||||
preinit_thread(L, g);
|
||||
g->allgc = obj2gco(L); /* by now, only object is the main thread */
|
||||
L->next = NULL;
|
||||
g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR;
|
||||
g->frealloc = f;
|
||||
g->ud = ud;
|
||||
g->warnf = NULL;
|
||||
@@ -420,3 +449,19 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Generate a warning from an error message
|
||||
*/
|
||||
void luaE_warnerror (lua_State *L, const char *where) {
|
||||
TValue *errobj = s2v(L->top - 1); /* error object */
|
||||
const char *msg = (ttisstring(errobj))
|
||||
? svalue(errobj)
|
||||
: "error object is not a string";
|
||||
/* produce warning "error in %s (%s)" (where, msg) */
|
||||
luaE_warning(L, "error in ", 1);
|
||||
luaE_warning(L, where, 1);
|
||||
luaE_warning(L, " (", 1);
|
||||
luaE_warning(L, msg, 1);
|
||||
luaE_warning(L, ")", 0);
|
||||
}
|
||||
|
||||
|
||||
84
lstate.h
84
lstate.h
@@ -64,28 +64,49 @@
|
||||
|
||||
/*
|
||||
** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of
|
||||
** how many "C calls" it can do in the C stack, to avoid C-stack overflow.
|
||||
** This count is very rough approximation; it considers only recursive
|
||||
** functions inside the interpreter, as non-recursive calls can be
|
||||
** considered using a fixed (although unknown) amount of stack space.
|
||||
** how many "C calls" it still can do in the C stack, to avoid C-stack
|
||||
** overflow. This count is very rough approximation; it considers only
|
||||
** recursive functions inside the interpreter, as non-recursive calls
|
||||
** can be considered using a fixed (although unknown) amount of stack
|
||||
** space.
|
||||
**
|
||||
** The count itself has two parts: the lower part is the count itself;
|
||||
** the higher part counts the number of non-yieldable calls in the stack.
|
||||
** The count has two parts: the lower part is the count itself; the
|
||||
** higher part counts the number of non-yieldable calls in the stack.
|
||||
** (They are together so that we can change both with one instruction.)
|
||||
**
|
||||
** Because calls to external C functions can use of unkown amount
|
||||
** Because calls to external C functions can use an unknown amount
|
||||
** of space (e.g., functions using an auxiliary buffer), calls
|
||||
** to these functions add more than one to the count.
|
||||
** to these functions add more than one to the count (see CSTACKCF).
|
||||
**
|
||||
** The proper count also includes the number of CallInfo structures
|
||||
** allocated by Lua, as a kind of "potential" calls. So, when Lua
|
||||
** calls a function (and "consumes" one CallInfo), it needs neither to
|
||||
** increment nor to check 'nCcalls', as its use of C stack is already
|
||||
** accounted for.
|
||||
** The proper count excludes the number of CallInfo structures allocated
|
||||
** by Lua, as a kind of "potential" calls. So, when Lua calls a function
|
||||
** (and "consumes" one CallInfo), it needs neither to decrement nor to
|
||||
** check 'nCcalls', as its use of C stack is already accounted for.
|
||||
*/
|
||||
|
||||
/* number of "C stack slots" used by an external C function */
|
||||
#define CSTACKCF 10
|
||||
|
||||
|
||||
/*
|
||||
** The C-stack size is sliced in the following zones:
|
||||
** - larger than CSTACKERR: normal stack;
|
||||
** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow;
|
||||
** - [CSTACKCF, CSTACKERRMARK]: error-handling zone;
|
||||
** - below CSTACKERRMARK: buffer zone to signal overflow during overflow;
|
||||
** (Because the counter can be decremented CSTACKCF at once, we need
|
||||
** the so called "buffer zones", with at least that size, to properly
|
||||
** detect a change from one zone to the next.)
|
||||
*/
|
||||
#define CSTACKERR (8 * CSTACKCF)
|
||||
#define CSTACKMARK (CSTACKERR - (CSTACKCF + 2))
|
||||
#define CSTACKERRMARK (CSTACKCF + 2)
|
||||
|
||||
|
||||
/* initial limit for the C-stack of threads */
|
||||
#define CSTACKTHREAD (2 * CSTACKERR)
|
||||
|
||||
|
||||
/* true if this thread does not have non-yieldable calls in the stack */
|
||||
#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0)
|
||||
|
||||
@@ -99,11 +120,11 @@
|
||||
/* Decrement the number of non-yieldable calls */
|
||||
#define decnny(L) ((L)->nCcalls -= 0x10000)
|
||||
|
||||
/* Increment the number of non-yieldable calls and nCcalls */
|
||||
#define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF)
|
||||
/* Increment the number of non-yieldable calls and decrement nCcalls */
|
||||
#define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF)
|
||||
|
||||
/* Decrement the number of non-yieldable calls and nCcalls */
|
||||
#define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF)
|
||||
/* Decrement the number of non-yieldable calls and increment nCcalls */
|
||||
#define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF)
|
||||
|
||||
|
||||
|
||||
@@ -152,7 +173,7 @@ typedef struct CallInfo {
|
||||
union {
|
||||
struct { /* only for Lua functions */
|
||||
const Instruction *savedpc;
|
||||
l_signalT trap;
|
||||
volatile l_signalT trap;
|
||||
int nextraargs; /* # of extra arguments in vararg functions */
|
||||
} l;
|
||||
struct { /* only for C functions */
|
||||
@@ -164,9 +185,9 @@ typedef struct CallInfo {
|
||||
union {
|
||||
int funcidx; /* called-function index */
|
||||
int nyield; /* number of values yielded */
|
||||
struct { /* info about transfered values (for call/return hooks) */
|
||||
unsigned short ftransfer; /* offset of first value transfered */
|
||||
unsigned short ntransfer; /* number of values transfered */
|
||||
struct { /* info about transferred values (for call/return hooks) */
|
||||
unsigned short ftransfer; /* offset of first value transferred */
|
||||
unsigned short ntransfer; /* number of values transferred */
|
||||
} transferinfo;
|
||||
} u2;
|
||||
short nresults; /* expected number of results from this function */
|
||||
@@ -250,6 +271,7 @@ typedef struct global_State {
|
||||
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
||||
lua_WarnFunction warnf; /* warning function */
|
||||
void *ud_warn; /* auxiliary data to 'warnf' */
|
||||
unsigned int Cstacklimit; /* current limit for the C stack */
|
||||
} global_State;
|
||||
|
||||
|
||||
@@ -278,7 +300,7 @@ struct lua_State {
|
||||
int stacksize;
|
||||
int basehookcount;
|
||||
int hookcount;
|
||||
l_signalT hookmask;
|
||||
volatile l_signalT hookmask;
|
||||
};
|
||||
|
||||
|
||||
@@ -305,16 +327,15 @@ union GCUnion {
|
||||
/* macros to convert a GCObject into a specific value */
|
||||
#define gco2ts(o) \
|
||||
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
|
||||
#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
|
||||
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
|
||||
#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
|
||||
#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u))
|
||||
#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l))
|
||||
#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c))
|
||||
#define gco2cl(o) \
|
||||
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
|
||||
#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
|
||||
#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
|
||||
#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
|
||||
#define gco2upv(o) \
|
||||
check_exp(novariant((o)->tt) == LUA_TUPVAL, &((cast_u(o))->upv))
|
||||
#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h))
|
||||
#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
|
||||
#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
|
||||
#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
|
||||
|
||||
|
||||
/*
|
||||
@@ -334,9 +355,10 @@ LUAI_FUNC void luaE_freeCI (lua_State *L);
|
||||
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
||||
LUAI_FUNC void luaE_enterCcall (lua_State *L);
|
||||
LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
|
||||
LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
|
||||
|
||||
|
||||
#define luaE_exitCcall(L) ((L)->nCcalls--)
|
||||
#define luaE_exitCcall(L) ((L)->nCcalls++)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
22
lstring.c
22
lstring.c
@@ -23,7 +23,7 @@
|
||||
|
||||
|
||||
/*
|
||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
||||
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to
|
||||
** compute its hash
|
||||
*/
|
||||
#if !defined(LUAI_HASHLIMIT)
|
||||
@@ -43,16 +43,16 @@
|
||||
*/
|
||||
int luaS_eqlngstr (TString *a, TString *b) {
|
||||
size_t len = a->u.lnglen;
|
||||
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
||||
lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
|
||||
return (a == b) || /* same instance or... */
|
||||
((len == b->u.lnglen) && /* equal length and ... */
|
||||
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
||||
}
|
||||
|
||||
|
||||
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed,
|
||||
size_t step) {
|
||||
unsigned int h = seed ^ cast_uint(l);
|
||||
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
||||
for (; l >= step; l -= step)
|
||||
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
||||
return h;
|
||||
@@ -60,9 +60,11 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||
|
||||
|
||||
unsigned int luaS_hashlongstr (TString *ts) {
|
||||
lua_assert(ts->tt == LUA_TLNGSTR);
|
||||
lua_assert(ts->tt == LUA_VLNGSTR);
|
||||
if (ts->extra == 0) { /* no hash? */
|
||||
ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
|
||||
size_t len = ts->u.lnglen;
|
||||
size_t step = (len >> LUAI_HASHLIMIT) + 1;
|
||||
ts->hash = luaS_hash(getstr(ts), len, ts->hash, step);
|
||||
ts->extra = 1; /* now it has its hash */
|
||||
}
|
||||
return ts->hash;
|
||||
@@ -165,7 +167,7 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
|
||||
|
||||
|
||||
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
||||
TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
|
||||
TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
|
||||
ts->u.lnglen = l;
|
||||
return ts;
|
||||
}
|
||||
@@ -199,7 +201,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||
TString *ts;
|
||||
global_State *g = G(L);
|
||||
stringtable *tb = &g->strt;
|
||||
unsigned int h = luaS_hash(str, l, g->seed);
|
||||
unsigned int h = luaS_hash(str, l, g->seed, 1);
|
||||
TString **list = &tb->hash[lmod(h, tb->size)];
|
||||
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
||||
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
||||
@@ -215,7 +217,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||
growstrtab(L, tb);
|
||||
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
|
||||
}
|
||||
ts = createstrobj(L, l, LUA_TSHRSTR, h);
|
||||
ts = createstrobj(L, l, LUA_VSHRSTR, h);
|
||||
memcpy(getstr(ts), str, l * sizeof(char));
|
||||
ts->shrlen = cast_byte(l);
|
||||
ts->u.hnext = *list;
|
||||
@@ -271,7 +273,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
|
||||
GCObject *o;
|
||||
if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
|
||||
luaM_toobig(L);
|
||||
o = luaC_newobj(L, LUA_TUSERDATA, sizeudata(nuvalue, s));
|
||||
o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
|
||||
u = gco2u(o);
|
||||
u->len = s;
|
||||
u->nuvalue = nuvalue;
|
||||
|
||||
13
lstring.h
13
lstring.h
@@ -19,7 +19,11 @@
|
||||
#define MEMERRMSG "not enough memory"
|
||||
|
||||
|
||||
#define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char))
|
||||
/*
|
||||
** Size of a TString: Size of the header plus space for the string
|
||||
** itself (including final '\0').
|
||||
*/
|
||||
#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char))
|
||||
|
||||
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
||||
(sizeof(s)/sizeof(char))-1))
|
||||
@@ -28,16 +32,17 @@
|
||||
/*
|
||||
** test whether a string is a reserved word
|
||||
*/
|
||||
#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0)
|
||||
#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0)
|
||||
|
||||
|
||||
/*
|
||||
** equality for short strings, which are always internalized
|
||||
*/
|
||||
#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b))
|
||||
#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b))
|
||||
|
||||
|
||||
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
||||
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l,
|
||||
unsigned int seed, size_t step);
|
||||
LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
|
||||
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||
|
||||
78
lstrlib.c
78
lstrlib.c
@@ -206,22 +206,38 @@ static int str_char (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int writer (lua_State *L, const void *b, size_t size, void *B) {
|
||||
(void)L;
|
||||
luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
|
||||
/*
|
||||
** Buffer to store the result of 'string.dump'. It must be initialized
|
||||
** after the call to 'lua_dump', to ensure that the function is on the
|
||||
** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might
|
||||
** push stuff.)
|
||||
*/
|
||||
struct str_Writer {
|
||||
int init; /* true iff buffer has been initialized */
|
||||
luaL_Buffer B;
|
||||
};
|
||||
|
||||
|
||||
static int writer (lua_State *L, const void *b, size_t size, void *ud) {
|
||||
struct str_Writer *state = (struct str_Writer *)ud;
|
||||
if (!state->init) {
|
||||
state->init = 1;
|
||||
luaL_buffinit(L, &state->B);
|
||||
}
|
||||
luaL_addlstring(&state->B, (const char *)b, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int str_dump (lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
struct str_Writer state;
|
||||
int strip = lua_toboolean(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_settop(L, 1);
|
||||
luaL_buffinit(L,&b);
|
||||
if (lua_dump(L, writer, &b, strip) != 0)
|
||||
lua_settop(L, 1); /* ensure function is on the top of the stack */
|
||||
state.init = 0;
|
||||
if (lua_dump(L, writer, &state, strip) != 0)
|
||||
return luaL_error(L, "unable to dump given function");
|
||||
luaL_pushresult(&b);
|
||||
luaL_pushresult(&state.B);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -233,6 +249,17 @@ static int str_dump (lua_State *L) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if defined(LUA_NOCVTS2N) /* { */
|
||||
|
||||
/* no coercion from strings to numbers */
|
||||
|
||||
static const luaL_Reg stringmetamethods[] = {
|
||||
{"__index", NULL}, /* placeholder */
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#else /* }{ */
|
||||
|
||||
static int tonum (lua_State *L, int arg) {
|
||||
if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */
|
||||
lua_pushvalue(L, arg);
|
||||
@@ -311,6 +338,8 @@ static const luaL_Reg stringmetamethods[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#endif /* } */
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
/*
|
||||
@@ -744,7 +773,7 @@ static int str_find_aux (lua_State *L, int find) {
|
||||
const char *p = luaL_checklstring(L, 2, &lp);
|
||||
size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
|
||||
if (init > ls) { /* start after string's end? */
|
||||
lua_pushnil(L); /* cannot find anything */
|
||||
luaL_pushfail(L); /* cannot find anything */
|
||||
return 1;
|
||||
}
|
||||
/* explicit request or no special characters? */
|
||||
@@ -779,7 +808,7 @@ static int str_find_aux (lua_State *L, int find) {
|
||||
}
|
||||
} while (s1++ < ms.src_end && !anchor);
|
||||
}
|
||||
lua_pushnil(L); /* not found */
|
||||
luaL_pushfail(L); /* not found */
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -975,7 +1004,7 @@ static int str_gsub (lua_State *L) {
|
||||
** 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)
|
||||
#define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1)
|
||||
|
||||
|
||||
/*
|
||||
@@ -1043,7 +1072,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
|
||||
** and '\0') + number of decimal digits to represent maxfloat (which
|
||||
** is maximum exponent + 1). (99+3+1, adding some extra, 110)
|
||||
*/
|
||||
#define MAX_ITEMF (110 + l_mathlim(MAX_10_EXP))
|
||||
#define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP))
|
||||
|
||||
|
||||
/*
|
||||
@@ -1058,7 +1087,10 @@ static int lua_number2strx (lua_State *L, char *buff, int sz,
|
||||
|
||||
|
||||
/* valid flags in a format specification */
|
||||
#define FLAGS "-+ #0"
|
||||
#if !defined(L_FMTFLAGS)
|
||||
#define L_FMTFLAGS "-+ #0"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** maximum size of each format specification (such as "%-099.99d")
|
||||
@@ -1156,8 +1188,8 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
|
||||
|
||||
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
|
||||
const char *p = strfrmt;
|
||||
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
|
||||
if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
|
||||
while (*p != '\0' && strchr(L_FMTFLAGS, *p) != NULL) p++; /* skip flags */
|
||||
if ((size_t)(p - strfrmt) >= sizeof(L_FMTFLAGS)/sizeof(char))
|
||||
luaL_error(L, "invalid format (repeated flags)");
|
||||
if (isdigit(uchar(*p))) p++; /* skip width */
|
||||
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
|
||||
@@ -1227,20 +1259,22 @@ static int str_format (lua_State *L) {
|
||||
nb = lua_number2strx(L, buff, maxitem, form,
|
||||
luaL_checknumber(L, arg));
|
||||
break;
|
||||
case 'e': case 'E': case 'f':
|
||||
case 'g': case 'G': {
|
||||
case 'f':
|
||||
maxitem = MAX_ITEMF; /* extra space for '%f' */
|
||||
buff = luaL_prepbuffsize(&b, maxitem);
|
||||
/* FALLTHROUGH */
|
||||
case 'e': case 'E': case 'g': case 'G': {
|
||||
lua_Number n = luaL_checknumber(L, arg);
|
||||
if (*(strfrmt - 1) == 'f' && l_mathop(fabs)(n) >= 1e100) {
|
||||
/* 'n' needs more than 99 digits */
|
||||
maxitem = MAX_ITEMF; /* extra space for '%f' */
|
||||
buff = luaL_prepbuffsize(&b, maxitem);
|
||||
}
|
||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||
nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
|
||||
break;
|
||||
}
|
||||
case 'p': {
|
||||
const void *p = lua_topointer(L, arg);
|
||||
if (p == NULL) { /* avoid calling 'printf' with argument NULL */
|
||||
p = "(null)"; /* result */
|
||||
form[strlen(form) - 1] = 's'; /* format it as a string */
|
||||
}
|
||||
nb = l_sprintf(buff, maxitem, form, p);
|
||||
break;
|
||||
}
|
||||
|
||||
101
ltable.c
101
ltable.c
@@ -88,8 +88,8 @@
|
||||
#define dummynode (&dummynode_)
|
||||
|
||||
static const Node dummynode_ = {
|
||||
{{NULL}, LUA_TEMPTY, /* value's value and type */
|
||||
LUA_TNIL, 0, {NULL}} /* key type, next, and key value */
|
||||
{{NULL}, LUA_VEMPTY, /* value's value and type */
|
||||
LUA_VNIL, 0, {NULL}} /* key type, next, and key value */
|
||||
};
|
||||
|
||||
|
||||
@@ -135,19 +135,21 @@ static int l_hashfloat (lua_Number n) {
|
||||
*/
|
||||
static Node *mainposition (const Table *t, int ktt, const Value *kvl) {
|
||||
switch (withvariant(ktt)) {
|
||||
case LUA_TNUMINT:
|
||||
case LUA_VNUMINT:
|
||||
return hashint(t, ivalueraw(*kvl));
|
||||
case LUA_TNUMFLT:
|
||||
case LUA_VNUMFLT:
|
||||
return hashmod(t, l_hashfloat(fltvalueraw(*kvl)));
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_VSHRSTR:
|
||||
return hashstr(t, tsvalueraw(*kvl));
|
||||
case LUA_TLNGSTR:
|
||||
case LUA_VLNGSTR:
|
||||
return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl)));
|
||||
case LUA_TBOOLEAN:
|
||||
return hashboolean(t, bvalueraw(*kvl));
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
case LUA_VFALSE:
|
||||
return hashboolean(t, 0);
|
||||
case LUA_VTRUE:
|
||||
return hashboolean(t, 1);
|
||||
case LUA_VLIGHTUSERDATA:
|
||||
return hashpointer(t, pvalueraw(*kvl));
|
||||
case LUA_TLCF:
|
||||
case LUA_VLCF:
|
||||
return hashpointer(t, fvalueraw(*kvl));
|
||||
default:
|
||||
return hashpointer(t, gcvalueraw(*kvl));
|
||||
@@ -155,6 +157,9 @@ static Node *mainposition (const Table *t, int ktt, const Value *kvl) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Returns the main position of an element given as a 'TValue'
|
||||
*/
|
||||
static Node *mainpositionTV (const Table *t, const TValue *key) {
|
||||
return mainposition(t, rawtt(key), valraw(key));
|
||||
}
|
||||
@@ -172,19 +177,17 @@ static int equalkey (const TValue *k1, const Node *n2) {
|
||||
if (rawtt(k1) != keytt(n2)) /* not the same variants? */
|
||||
return 0; /* cannot be same key */
|
||||
switch (ttypetag(k1)) {
|
||||
case LUA_TNIL:
|
||||
case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
|
||||
return 1;
|
||||
case LUA_TNUMINT:
|
||||
case LUA_VNUMINT:
|
||||
return (ivalue(k1) == keyival(n2));
|
||||
case LUA_TNUMFLT:
|
||||
case LUA_VNUMFLT:
|
||||
return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
|
||||
case LUA_TBOOLEAN:
|
||||
return bvalue(k1) == bvalueraw(keyval(n2));
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
case LUA_VLIGHTUSERDATA:
|
||||
return pvalue(k1) == pvalueraw(keyval(n2));
|
||||
case LUA_TLCF:
|
||||
case LUA_VLCF:
|
||||
return fvalue(k1) == fvalueraw(keyval(n2));
|
||||
case LUA_TLNGSTR:
|
||||
case LUA_VLNGSTR:
|
||||
return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
|
||||
default:
|
||||
return gcvalue(k1) == gcvalueraw(keyval(n2));
|
||||
@@ -577,7 +580,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
|
||||
|
||||
|
||||
Table *luaH_new (lua_State *L) {
|
||||
GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table));
|
||||
GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
|
||||
Table *t = gco2t(o);
|
||||
t->metatable = NULL;
|
||||
t->flags = cast_byte(~0);
|
||||
@@ -623,7 +626,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
else if (ttisfloat(key)) {
|
||||
lua_Number f = fltvalue(key);
|
||||
lua_Integer k;
|
||||
if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */
|
||||
if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
|
||||
setivalue(&aux, k);
|
||||
key = &aux; /* insert it as an integer */
|
||||
}
|
||||
@@ -707,7 +710,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
|
||||
*/
|
||||
const TValue *luaH_getshortstr (Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
lua_assert(key->tt == LUA_TSHRSTR);
|
||||
lua_assert(key->tt == LUA_VSHRSTR);
|
||||
for (;;) { /* check whether 'key' is somewhere in the chain */
|
||||
if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
|
||||
return gval(n); /* that's it */
|
||||
@@ -722,7 +725,7 @@ const TValue *luaH_getshortstr (Table *t, TString *key) {
|
||||
|
||||
|
||||
const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
if (key->tt == LUA_TSHRSTR)
|
||||
if (key->tt == LUA_VSHRSTR)
|
||||
return luaH_getshortstr(t, key);
|
||||
else { /* for long strings, use generic case */
|
||||
TValue ko;
|
||||
@@ -737,12 +740,12 @@ const TValue *luaH_getstr (Table *t, TString *key) {
|
||||
*/
|
||||
const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
switch (ttypetag(key)) {
|
||||
case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
|
||||
case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
|
||||
case LUA_TNIL: return &absentkey;
|
||||
case LUA_TNUMFLT: {
|
||||
case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key));
|
||||
case LUA_VNUMINT: return luaH_getint(t, ivalue(key));
|
||||
case LUA_VNIL: return &absentkey;
|
||||
case LUA_VNUMFLT: {
|
||||
lua_Integer k;
|
||||
if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */
|
||||
if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
|
||||
return luaH_getint(t, k); /* use specialized version */
|
||||
/* else... */
|
||||
} /* FALLTHROUGH */
|
||||
@@ -833,39 +836,41 @@ static unsigned int binsearch (const TValue *array, unsigned int i,
|
||||
** and 'maxinteger' if t[maxinteger] is present.)
|
||||
** (In the next explanation, we use Lua indices, that is, with base 1.
|
||||
** The code itself uses base 0 when indexing the array part of the table.)
|
||||
** The code starts with 'limit', a position in the array part that may
|
||||
** be a boundary.
|
||||
** The code starts with 'limit = t->alimit', a position in the array
|
||||
** part that may be a boundary.
|
||||
**
|
||||
** (1) If 't[limit]' is empty, there must be a boundary before it.
|
||||
** As a common case (e.g., after 't[#t]=nil'), check whether 'hint-1'
|
||||
** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1'
|
||||
** is present. If so, it is a boundary. Otherwise, do a binary search
|
||||
** between 0 and limit to find a boundary. In both cases, try to
|
||||
** use this boundary as the new 'limit', as a hint for the next call.
|
||||
** use this boundary as the new 'alimit', as a hint for the next call.
|
||||
**
|
||||
** (2) If 't[limit]' is not empty and the array has more elements
|
||||
** after 'limit', try to find a boundary there. Again, try first
|
||||
** the special case (which should be quite frequent) where 'limit+1'
|
||||
** is empty, so that 'limit' is a boundary. Otherwise, check the
|
||||
** last element of the array part (set it as a new limit). If it is empty,
|
||||
** there must be a boundary between the old limit (present) and the new
|
||||
** limit (absent), which is found with a binary search. (This boundary
|
||||
** always can be a new limit.)
|
||||
** last element of the array part. If it is empty, there must be a
|
||||
** boundary between the old limit (present) and the last element
|
||||
** (absent), which is found with a binary search. (This boundary always
|
||||
** can be a new limit.)
|
||||
**
|
||||
** (3) The last case is when there are no elements in the array part
|
||||
** (limit == 0) or its last element (the new limit) is present.
|
||||
** In this case, must check the hash part. If there is no hash part,
|
||||
** the boundary is 0. Otherwise, if 'limit+1' is absent, 'limit' is
|
||||
** a boundary. Finally, if 'limit+1' is present, call 'hash_search'
|
||||
** to find a boundary in the hash part of the table. (In those
|
||||
** cases, the boundary is not inside the array part, and therefore
|
||||
** cannot be used as a new limit.)
|
||||
** In this case, must check the hash part. If there is no hash part
|
||||
** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call
|
||||
** 'hash_search' to find a boundary in the hash part of the table.
|
||||
** (In those cases, the boundary is not inside the array part, and
|
||||
** therefore cannot be used as a new limit.)
|
||||
*/
|
||||
lua_Unsigned luaH_getn (Table *t) {
|
||||
unsigned int limit = t->alimit;
|
||||
if (limit > 0 && isempty(&t->array[limit - 1])) {
|
||||
/* (1) there must be a boundary before 'limit' */
|
||||
if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
|
||||
/* there must be a boundary before 'limit' */
|
||||
if (limit >= 2 && !isempty(&t->array[limit - 2])) {
|
||||
/* 'limit - 1' is a boundary; can it be a new limit? */
|
||||
if (ispow2realasize(t) && !ispow2(limit - 1)) {
|
||||
t->alimit = limit - 1;
|
||||
setnorealasize(t);
|
||||
setnorealasize(t); /* now 'alimit' is not the real size */
|
||||
}
|
||||
return limit - 1;
|
||||
}
|
||||
@@ -880,8 +885,8 @@ lua_Unsigned luaH_getn (Table *t) {
|
||||
}
|
||||
}
|
||||
/* 'limit' is zero or present in table */
|
||||
if (!limitequalsasize(t)) {
|
||||
/* (2) 'limit' > 0 and array has more elements after 'limit' */
|
||||
if (!limitequalsasize(t)) { /* (2)? */
|
||||
/* 'limit' > 0 and array has more elements after 'limit' */
|
||||
if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
|
||||
return limit; /* this is the boundary */
|
||||
/* else, try last element in the array */
|
||||
@@ -899,7 +904,7 @@ lua_Unsigned luaH_getn (Table *t) {
|
||||
lua_assert(limit == luaH_realasize(t) &&
|
||||
(limit == 0 || !isempty(&t->array[limit - 1])));
|
||||
if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
|
||||
return limit; /* 'limit + 1' is absent... */
|
||||
return limit; /* 'limit + 1' is absent */
|
||||
else /* 'limit + 1' is also present */
|
||||
return hash_search(t, limit);
|
||||
}
|
||||
@@ -908,6 +913,8 @@ lua_Unsigned luaH_getn (Table *t) {
|
||||
|
||||
#if defined(LUA_DEBUG)
|
||||
|
||||
/* export these functions for the test library */
|
||||
|
||||
Node *luaH_mainposition (const Table *t, const TValue *key) {
|
||||
return mainpositionTV(t, key);
|
||||
}
|
||||
|
||||
2
ltable.h
2
ltable.h
@@ -27,7 +27,7 @@
|
||||
|
||||
|
||||
/* returns the Node, given the value of a table entry */
|
||||
#define nodefromval(v) cast(Node *, (v))
|
||||
#define nodefromval(v) cast(Node *, (v))
|
||||
|
||||
|
||||
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
||||
|
||||
@@ -338,7 +338,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
||||
|
||||
|
||||
/*
|
||||
** QuickSort algorithm (recursive function)
|
||||
** Quicksort algorithm (recursive function)
|
||||
*/
|
||||
static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||
unsigned int rnd) {
|
||||
|
||||
172
ltests.c
172
ltests.c
@@ -62,8 +62,10 @@ static void pushobject (lua_State *L, const TValue *o) {
|
||||
}
|
||||
|
||||
|
||||
static void badexit (const char *fmt, const char *s) {
|
||||
fprintf(stderr, fmt, s);
|
||||
static void badexit (const char *fmt, const char *s1, const char *s2) {
|
||||
fprintf(stderr, fmt, s1);
|
||||
if (s2)
|
||||
fprintf(stderr, "extra info: %s\n", s2);
|
||||
/* avoid assertion failures when exiting */
|
||||
l_memcontrol.numblocks = l_memcontrol.total = 0;
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -71,40 +73,81 @@ static void badexit (const char *fmt, const char *s) {
|
||||
|
||||
|
||||
static int tpanic (lua_State *L) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "error object is not a string";
|
||||
return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
lua_tostring(L, -1)),
|
||||
msg, NULL),
|
||||
0); /* do not return to Lua */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Warning function for tests. Fist, it concatenates all parts of
|
||||
** a warning in buffer 'buff'. Then:
|
||||
** - messages starting with '#' are shown on standard output (used to
|
||||
** test explicit warnings);
|
||||
** - messages containing '@' are stored in global '_WARN' (used to test
|
||||
** errors that generate warnings);
|
||||
** Warning function for tests. First, it concatenates all parts of
|
||||
** a warning in buffer 'buff'. Then, it has three modes:
|
||||
** - 0.normal: messages starting with '#' are shown on standard output;
|
||||
** - other messages abort the tests (they represent real warning
|
||||
** conditions; the standard tests should not generate these conditions
|
||||
** unexpectedly).
|
||||
** unexpectedly);
|
||||
** - 1.allow: all messages are shown;
|
||||
** - 2.store: all warnings go to the global '_WARN';
|
||||
*/
|
||||
static void warnf (void *ud, const char *msg, int tocont) {
|
||||
lua_State *L = cast(lua_State *, ud);
|
||||
static char buff[200] = ""; /* should be enough for tests... */
|
||||
static int onoff = 0;
|
||||
static int mode = 0; /* start in normal mode */
|
||||
static int lasttocont = 0;
|
||||
if (!lasttocont && !tocont && *msg == '@') { /* control message? */
|
||||
if (buff[0] != '\0')
|
||||
badexit("Control warning during warning: %s\naborting...\n", msg, buff);
|
||||
if (strcmp(msg, "@off") == 0)
|
||||
onoff = 0;
|
||||
else if (strcmp(msg, "@on") == 0)
|
||||
onoff = 1;
|
||||
else if (strcmp(msg, "@normal") == 0)
|
||||
mode = 0;
|
||||
else if (strcmp(msg, "@allow") == 0)
|
||||
mode = 1;
|
||||
else if (strcmp(msg, "@store") == 0)
|
||||
mode = 2;
|
||||
else
|
||||
badexit("Invalid control warning in test mode: %s\naborting...\n",
|
||||
msg, NULL);
|
||||
return;
|
||||
}
|
||||
lasttocont = tocont;
|
||||
if (strlen(msg) >= sizeof(buff) - strlen(buff))
|
||||
badexit("%s", "warnf-buffer overflow");
|
||||
badexit("warnf-buffer overflow (%s)\n", msg, buff);
|
||||
strcat(buff, msg); /* add new message to current warning */
|
||||
if (!tocont) { /* message finished? */
|
||||
if (buff[0] == '#') /* expected warning? */
|
||||
printf("Expected Lua warning: %s\n", buff); /* print it */
|
||||
else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */
|
||||
lua_State *L = cast(lua_State *, ud);
|
||||
lua_unlock(L);
|
||||
lua_pushstring(L, buff);
|
||||
lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
|
||||
lua_lock(L);
|
||||
lua_unlock(L);
|
||||
if (lua_getglobal(L, "_WARN") == LUA_TNIL)
|
||||
lua_pop(L, 1); /* ok, no previous unexpected warning */
|
||||
else {
|
||||
badexit("Unhandled warning in store mode: %s\naborting...\n",
|
||||
lua_tostring(L, -1), buff);
|
||||
}
|
||||
lua_lock(L);
|
||||
switch (mode) {
|
||||
case 0: { /* normal */
|
||||
if (buff[0] != '#' && onoff) /* unexpected warning? */
|
||||
badexit("Unexpected warning in test mode: %s\naborting...\n",
|
||||
buff, NULL);
|
||||
} /* FALLTHROUGH */
|
||||
case 1: { /* allow */
|
||||
if (onoff)
|
||||
fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
|
||||
break;
|
||||
}
|
||||
case 2: { /* store */
|
||||
lua_unlock(L);
|
||||
lua_pushstring(L, buff);
|
||||
lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
|
||||
lua_lock(L);
|
||||
buff[0] = '\0'; /* prepare buffer for next warning */
|
||||
break;
|
||||
}
|
||||
}
|
||||
else /* a real warning; should not happen during tests */
|
||||
badexit("Unexpected warning in test mode: %s\naborting...\n", buff);
|
||||
buff[0] = '\0'; /* prepare buffer for next warning */
|
||||
}
|
||||
}
|
||||
@@ -261,7 +304,7 @@ static void printobj (global_State *g, GCObject *o) {
|
||||
ttypename(novariant(o->tt)), (void *)o,
|
||||
isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
|
||||
"ns01oTt"[getage(o)], o->marked);
|
||||
if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR)
|
||||
if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR)
|
||||
printf(" '%s'", getstr(gco2ts(o)));
|
||||
}
|
||||
|
||||
@@ -377,53 +420,54 @@ static void checkstack (global_State *g, lua_State *L1) {
|
||||
CallInfo *ci;
|
||||
UpVal *uv;
|
||||
lua_assert(!isdead(g, L1));
|
||||
if (L1->stack == NULL) { /* incomplete thread? */
|
||||
lua_assert(L1->stacksize == 0 && L1->openupval == NULL &&
|
||||
L1->ci == NULL);
|
||||
return;
|
||||
}
|
||||
for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
|
||||
lua_assert(upisopen(uv)); /* must be open */
|
||||
for (ci = L1->ci; ci != NULL; ci = ci->previous) {
|
||||
lua_assert(ci->top <= L1->stack_last);
|
||||
lua_assert(lua_checkpc(ci));
|
||||
}
|
||||
if (L1->stack) { /* complete thread? */
|
||||
for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
|
||||
checkliveness(L1, s2v(o)); /* entire stack must have valid values */
|
||||
}
|
||||
else lua_assert(L1->stacksize == 0);
|
||||
for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++)
|
||||
checkliveness(L1, s2v(o)); /* entire stack must have valid values */
|
||||
}
|
||||
|
||||
|
||||
static void checkrefs (global_State *g, GCObject *o) {
|
||||
switch (o->tt) {
|
||||
case LUA_TUSERDATA: {
|
||||
case LUA_VUSERDATA: {
|
||||
checkudata(g, gco2u(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TUPVAL:
|
||||
case LUA_TUPVALTBC: {
|
||||
case LUA_VUPVAL: {
|
||||
checkvalref(g, o, gco2upv(o)->v);
|
||||
break;
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
case LUA_VTABLE: {
|
||||
checktable(g, gco2t(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TTHREAD: {
|
||||
case LUA_VTHREAD: {
|
||||
checkstack(g, gco2th(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TLCL: {
|
||||
case LUA_VLCL: {
|
||||
checkLclosure(g, gco2lcl(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TCCL: {
|
||||
case LUA_VCCL: {
|
||||
checkCclosure(g, gco2ccl(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TPROTO: {
|
||||
case LUA_VPROTO: {
|
||||
checkproto(g, gco2p(o));
|
||||
break;
|
||||
}
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR: {
|
||||
case LUA_VSHRSTR:
|
||||
case LUA_VLNGSTR: {
|
||||
lua_assert(!isgray(o)); /* strings are never gray */
|
||||
break;
|
||||
}
|
||||
@@ -435,7 +479,7 @@ static void checkrefs (global_State *g, GCObject *o) {
|
||||
/*
|
||||
** Check consistency of an object:
|
||||
** - Dead objects can only happen in the 'allgc' list during a sweep
|
||||
** phase (controled by the caller through 'maybedead').
|
||||
** phase (controlled by the caller through 'maybedead').
|
||||
** - During pause, all objects must be white.
|
||||
** - In generational mode:
|
||||
** * objects must be old enough for their lists ('listage').
|
||||
@@ -456,8 +500,8 @@ static void checkobject (global_State *g, GCObject *o, int maybedead,
|
||||
lua_assert(isblack(o) ||
|
||||
getage(o) == G_TOUCHED1 ||
|
||||
getage(o) == G_OLD0 ||
|
||||
o->tt == LUA_TTHREAD ||
|
||||
(o->tt == LUA_TUPVAL && upisopen(gco2upv(o))));
|
||||
o->tt == LUA_VTHREAD ||
|
||||
(o->tt == LUA_VUPVAL && upisopen(gco2upv(o))));
|
||||
}
|
||||
}
|
||||
checkrefs(g, o);
|
||||
@@ -470,11 +514,15 @@ static void checkgraylist (global_State *g, GCObject *o) {
|
||||
while (o) {
|
||||
lua_assert(isgray(o) || getage(o) == G_TOUCHED2);
|
||||
switch (o->tt) {
|
||||
case LUA_TTABLE: o = gco2t(o)->gclist; break;
|
||||
case LUA_TLCL: o = gco2lcl(o)->gclist; break;
|
||||
case LUA_TCCL: o = gco2ccl(o)->gclist; break;
|
||||
case LUA_TTHREAD: o = gco2th(o)->gclist; break;
|
||||
case LUA_TPROTO: o = gco2p(o)->gclist; break;
|
||||
case LUA_VTABLE: o = gco2t(o)->gclist; break;
|
||||
case LUA_VLCL: o = gco2lcl(o)->gclist; break;
|
||||
case LUA_VCCL: o = gco2ccl(o)->gclist; break;
|
||||
case LUA_VTHREAD: o = gco2th(o)->gclist; break;
|
||||
case LUA_VPROTO: o = gco2p(o)->gclist; break;
|
||||
case LUA_VUSERDATA:
|
||||
lua_assert(gco2u(o)->nuvalue > 0);
|
||||
o = gco2u(o)->gclist;
|
||||
break;
|
||||
default: lua_assert(0); /* other objects cannot be in a gray list */
|
||||
}
|
||||
}
|
||||
@@ -529,7 +577,7 @@ int lua_checkmemory (lua_State *L) {
|
||||
|
||||
/* check 'fixedgc' list */
|
||||
for (o = g->fixedgc; o != NULL; o = o->next) {
|
||||
lua_assert(o->tt == LUA_TSHRSTR && isgray(o) && getage(o) == G_OLD);
|
||||
lua_assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD);
|
||||
}
|
||||
|
||||
/* check 'allgc' list */
|
||||
@@ -543,7 +591,7 @@ int lua_checkmemory (lua_State *L) {
|
||||
for (o = g->tobefnz; o != NULL; o = o->next) {
|
||||
checkobject(g, o, 0, G_NEW);
|
||||
lua_assert(tofinalize(o));
|
||||
lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE);
|
||||
lua_assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -742,7 +790,7 @@ static int mem_query (lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return luaL_error(L, "unkown type '%s'", t);
|
||||
return luaL_error(L, "unknown type '%s'", t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1103,14 +1151,6 @@ static int doremote (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int int2fb_aux (lua_State *L) {
|
||||
int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1));
|
||||
lua_pushinteger(L, b);
|
||||
lua_pushinteger(L, (unsigned int)luaO_fb2int(b));
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
static int log2_aux (lua_State *L) {
|
||||
unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
|
||||
lua_pushinteger(L, luaO_ceillog2(x));
|
||||
@@ -1488,6 +1528,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
else if EQ("pushfstringP") {
|
||||
lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
|
||||
}
|
||||
else if EQ("rawget") {
|
||||
int t = getindex;
|
||||
lua_rawget(L1, t);
|
||||
}
|
||||
else if EQ("rawgeti") {
|
||||
int t = getindex;
|
||||
lua_rawgeti(L1, t, getnum);
|
||||
@@ -1496,6 +1540,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
int t = getindex;
|
||||
lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
||||
}
|
||||
else if EQ("rawset") {
|
||||
int t = getindex;
|
||||
lua_rawset(L1, t);
|
||||
}
|
||||
else if EQ("rawseti") {
|
||||
int t = getindex;
|
||||
lua_rawseti(L1, t, getnum);
|
||||
}
|
||||
else if EQ("rawsetp") {
|
||||
int t = getindex;
|
||||
lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
|
||||
@@ -1538,6 +1590,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
const char *s = getstring;
|
||||
lua_setfield(L1, t, s);
|
||||
}
|
||||
else if EQ("seti") {
|
||||
int t = getindex;
|
||||
lua_seti(L1, t, getnum);
|
||||
}
|
||||
else if EQ("setglobal") {
|
||||
const char *s = getstring;
|
||||
lua_setglobal(L1, s);
|
||||
@@ -1565,6 +1621,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
|
||||
else if EQ("error") {
|
||||
lua_error(L1);
|
||||
}
|
||||
else if EQ("abort") {
|
||||
abort();
|
||||
}
|
||||
else if EQ("throw") {
|
||||
#if defined(__cplusplus)
|
||||
static struct X { int x; } x;
|
||||
@@ -1764,7 +1823,6 @@ static const struct luaL_Reg tests_funcs[] = {
|
||||
{"pobj", gc_printobj},
|
||||
{"getref", getref},
|
||||
{"hash", hash_query},
|
||||
{"int2fb", int2fb_aux},
|
||||
{"log2", log2_aux},
|
||||
{"limits", get_limits},
|
||||
{"listcode", listcode},
|
||||
|
||||
21
ltests.h
21
ltests.h
@@ -25,13 +25,10 @@
|
||||
#define lua_assert(c) assert(c)
|
||||
|
||||
|
||||
/* include opcode names */
|
||||
#define LUAI_DEFOPNAMES
|
||||
|
||||
|
||||
/* compiled with -O0, Lua uses a lot of C stack space... */
|
||||
#undef LUAI_MAXCSTACK
|
||||
#define LUAI_MAXCSTACK 400
|
||||
#define LUAI_MAXCSTACK 400
|
||||
|
||||
/* to avoid warnings, and to make sure value is really unused */
|
||||
#define UNUSED(x) (x=0, (void)(x))
|
||||
@@ -61,7 +58,7 @@ typedef struct Memcontrol {
|
||||
unsigned long maxmem;
|
||||
unsigned long memlimit;
|
||||
unsigned long countlimit;
|
||||
unsigned long objcount[LUA_NUMTAGS];
|
||||
unsigned long objcount[LUA_NUMTYPES];
|
||||
} Memcontrol;
|
||||
|
||||
LUA_API Memcontrol l_memcontrol;
|
||||
@@ -121,18 +118,22 @@ LUA_API void *debug_realloc (void *ud, void *block,
|
||||
#define MINSTRTABSIZE 2
|
||||
#define MAXIWTHABS 3
|
||||
|
||||
#define STRCACHE_N 23
|
||||
#define STRCACHE_M 5
|
||||
|
||||
#undef LUAI_USER_ALIGNMENT_T
|
||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||
|
||||
|
||||
/* make stack-overflow tests run faster */
|
||||
#undef LUAI_MAXSTACK
|
||||
#define LUAI_MAXSTACK 50000
|
||||
|
||||
|
||||
#undef LUAI_USER_ALIGNMENT_T
|
||||
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||
/* force Lua to use its own implementations */
|
||||
#undef lua_strx2number
|
||||
#undef lua_number2strx
|
||||
|
||||
|
||||
#define STRCACHE_N 23
|
||||
#define STRCACHE_M 5
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
33
ltm.c
33
ltm.c
@@ -27,7 +27,7 @@
|
||||
|
||||
static const char udatatypename[] = "userdata";
|
||||
|
||||
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
||||
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
|
||||
"no value",
|
||||
"nil", "boolean", udatatypename, "number",
|
||||
"string", "table", "function", udatatypename, "thread",
|
||||
@@ -149,9 +149,6 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
StkId res, TMS event) {
|
||||
if (!callbinTM(L, p1, p2, res, event)) {
|
||||
switch (event) {
|
||||
case TM_CONCAT:
|
||||
luaG_concaterror(L, p1, p2);
|
||||
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
|
||||
case TM_BAND: case TM_BOR: case TM_BXOR:
|
||||
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
||||
if (ttisnumber(p1) && ttisnumber(p2))
|
||||
@@ -167,9 +164,16 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
}
|
||||
|
||||
|
||||
void luaT_tryconcatTM (lua_State *L) {
|
||||
StkId top = L->top;
|
||||
if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT))
|
||||
luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
|
||||
}
|
||||
|
||||
|
||||
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
StkId res, int inv, TMS event) {
|
||||
if (inv)
|
||||
int flip, StkId res, TMS event) {
|
||||
if (flip)
|
||||
luaT_trybinTM(L, p2, p1, res, event);
|
||||
else
|
||||
luaT_trybinTM(L, p1, p2, res, event);
|
||||
@@ -177,13 +181,22 @@ void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
|
||||
|
||||
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
||||
int inv, StkId res, TMS event) {
|
||||
int flip, StkId res, TMS event) {
|
||||
TValue aux;
|
||||
setivalue(&aux, i2);
|
||||
luaT_trybinassocTM(L, p1, &aux, res, inv, event);
|
||||
luaT_trybinassocTM(L, p1, &aux, flip, res, event);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Calls an order tag method.
|
||||
** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old
|
||||
** behavior: if there is no '__le', try '__lt', based on l <= r iff
|
||||
** !(r < l) (assuming a total order). If the metamethod yields during
|
||||
** this substitution, the continuation has to know about it (to negate
|
||||
** the result of r<l); bit CIST_LEQ in the call status keeps that
|
||||
** information.
|
||||
*/
|
||||
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
TMS event) {
|
||||
if (callbinTM(L, p1, p2, L->top, event)) /* try original event */
|
||||
@@ -205,14 +218,14 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
|
||||
|
||||
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
||||
int inv, int isfloat, TMS event) {
|
||||
int flip, int isfloat, TMS event) {
|
||||
TValue aux; const TValue *p2;
|
||||
if (isfloat) {
|
||||
setfltvalue(&aux, cast_num(v2));
|
||||
}
|
||||
else
|
||||
setivalue(&aux, v2);
|
||||
if (inv) { /* arguments were exchanged? */
|
||||
if (flip) { /* arguments were exchanged? */
|
||||
p2 = p1; p1 = &aux; /* correct them */
|
||||
}
|
||||
else
|
||||
|
||||
5
ltm.h
5
ltm.h
@@ -59,7 +59,7 @@ typedef enum {
|
||||
|
||||
#define ttypename(x) luaT_typenames_[(x) + 1]
|
||||
|
||||
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTAGS];)
|
||||
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];)
|
||||
|
||||
|
||||
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
|
||||
@@ -75,8 +75,9 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f,
|
||||
const TValue *p1, const TValue *p2, StkId p3);
|
||||
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||
StkId res, TMS event);
|
||||
LUAI_FUNC void luaT_tryconcatTM (lua_State *L);
|
||||
LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1,
|
||||
const TValue *p2, StkId res, int inv, TMS event);
|
||||
const TValue *p2, int inv, StkId res, TMS event);
|
||||
LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
||||
int inv, StkId res, TMS event);
|
||||
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
|
||||
|
||||
39
lua.c
39
lua.c
@@ -54,8 +54,9 @@ static void lstop (lua_State *L, lua_Debug *ar) {
|
||||
** interpreter.
|
||||
*/
|
||||
static void laction (int i) {
|
||||
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
||||
signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
||||
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
||||
lua_sethook(globalL, lstop, flag, 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +74,7 @@ static void print_usage (const char *badoption) {
|
||||
" -l name require library 'name' into global 'name'\n"
|
||||
" -v show version information\n"
|
||||
" -E ignore environment variables\n"
|
||||
" -W turn warnings on\n"
|
||||
" -- stop handling options\n"
|
||||
" - stop handling options and execute stdin\n"
|
||||
,
|
||||
@@ -259,14 +261,18 @@ static int collectargs (char **argv, int *first) {
|
||||
case '\0': /* '-' */
|
||||
return args; /* script "name" is '-' */
|
||||
case 'E':
|
||||
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
||||
if (argv[i][2] != '\0') /* extra characters? */
|
||||
return has_error; /* invalid option */
|
||||
args |= has_E;
|
||||
break;
|
||||
case 'W':
|
||||
if (argv[i][2] != '\0') /* extra characters? */
|
||||
return has_error; /* invalid option */
|
||||
break;
|
||||
case 'i':
|
||||
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
||||
case 'v':
|
||||
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
||||
if (argv[i][2] != '\0') /* extra characters? */
|
||||
return has_error; /* invalid option */
|
||||
args |= has_v;
|
||||
break;
|
||||
@@ -289,7 +295,8 @@ static int collectargs (char **argv, int *first) {
|
||||
|
||||
|
||||
/*
|
||||
** Processes options 'e' and 'l', which involve running Lua code.
|
||||
** Processes options 'e' and 'l', which involve running Lua code, and
|
||||
** 'W', which also affects the state.
|
||||
** Returns 0 if some code raises an error.
|
||||
*/
|
||||
static int runargs (lua_State *L, char **argv, int n) {
|
||||
@@ -297,15 +304,21 @@ static int runargs (lua_State *L, char **argv, int n) {
|
||||
for (i = 1; i < n; i++) {
|
||||
int option = argv[i][1];
|
||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||
if (option == 'e' || option == 'l') {
|
||||
int status;
|
||||
const char *extra = argv[i] + 2; /* both options need an argument */
|
||||
if (*extra == '\0') extra = argv[++i];
|
||||
lua_assert(extra != NULL);
|
||||
status = (option == 'e')
|
||||
? dostring(L, extra, "=(command line)")
|
||||
: dolibrary(L, extra);
|
||||
if (status != LUA_OK) return 0;
|
||||
switch (option) {
|
||||
case 'e': case 'l': {
|
||||
int status;
|
||||
const char *extra = argv[i] + 2; /* both options need an argument */
|
||||
if (*extra == '\0') extra = argv[++i];
|
||||
lua_assert(extra != NULL);
|
||||
status = (option == 'e')
|
||||
? dostring(L, extra, "=(command line)")
|
||||
: dolibrary(L, extra);
|
||||
if (status != LUA_OK) return 0;
|
||||
break;
|
||||
}
|
||||
case 'W':
|
||||
lua_warning(L, "@on", 0); /* warnings on */
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
|
||||
9
lua.h
9
lua.h
@@ -25,7 +25,7 @@
|
||||
|
||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2019 Lua.org, PUC-Rio"
|
||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio"
|
||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ typedef struct lua_State lua_State;
|
||||
#define LUA_TUSERDATA 7
|
||||
#define LUA_TTHREAD 8
|
||||
|
||||
#define LUA_NUMTAGS 9
|
||||
#define LUA_NUMTYPES 9
|
||||
|
||||
|
||||
|
||||
@@ -412,6 +412,8 @@ LUA_API void (lua_toclose) (lua_State *L, int idx);
|
||||
#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
|
||||
#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
|
||||
|
||||
#define LUA_NUMTAGS LUA_NUMTYPES
|
||||
|
||||
/* }============================================================== */
|
||||
|
||||
/*
|
||||
@@ -462,6 +464,7 @@ LUA_API lua_Hook (lua_gethook) (lua_State *L);
|
||||
LUA_API int (lua_gethookmask) (lua_State *L);
|
||||
LUA_API int (lua_gethookcount) (lua_State *L);
|
||||
|
||||
LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit);
|
||||
|
||||
struct lua_Debug {
|
||||
int event;
|
||||
@@ -488,7 +491,7 @@ struct lua_Debug {
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (C) 1994-2019 Lua.org, PUC-Rio.
|
||||
* Copyright (C) 1994-2020 Lua.org, PUC-Rio.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
|
||||
12
luaconf.h
12
luaconf.h
@@ -47,7 +47,7 @@
|
||||
** (It will crash with a limit too high.)
|
||||
*/
|
||||
#if !defined(LUAI_MAXCSTACK)
|
||||
#define LUAI_MAXCSTACK 2200
|
||||
#define LUAI_MAXCSTACK 2000
|
||||
#endif
|
||||
|
||||
|
||||
@@ -398,7 +398,7 @@
|
||||
@@ LUA_NUMBER is the floating-point type used by Lua.
|
||||
@@ LUAI_UACNUMBER is the result of a 'default argument promotion'
|
||||
@@ over a floating number.
|
||||
@@ l_mathlim(x) corrects limit name 'x' to the proper float type
|
||||
@@ l_floatatt(x) corrects float attribute 'x' to the proper float type
|
||||
** by prefixing it with one of FLT/DBL/LDBL.
|
||||
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
||||
@@ LUA_NUMBER_FMT is the format for writing floats.
|
||||
@@ -437,7 +437,7 @@
|
||||
|
||||
#define LUA_NUMBER float
|
||||
|
||||
#define l_mathlim(n) (FLT_##n)
|
||||
#define l_floatatt(n) (FLT_##n)
|
||||
|
||||
#define LUAI_UACNUMBER double
|
||||
|
||||
@@ -453,7 +453,7 @@
|
||||
|
||||
#define LUA_NUMBER long double
|
||||
|
||||
#define l_mathlim(n) (LDBL_##n)
|
||||
#define l_floatatt(n) (LDBL_##n)
|
||||
|
||||
#define LUAI_UACNUMBER long double
|
||||
|
||||
@@ -468,7 +468,7 @@
|
||||
|
||||
#define LUA_NUMBER double
|
||||
|
||||
#define l_mathlim(n) (DBL_##n)
|
||||
#define l_floatatt(n) (DBL_##n)
|
||||
|
||||
#define LUAI_UACNUMBER double
|
||||
|
||||
@@ -493,7 +493,7 @@
|
||||
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
|
||||
**
|
||||
@@ LUAI_UACINT is the result of a 'default argument promotion'
|
||||
@@ over a lUA_INTEGER.
|
||||
@@ over a LUA_INTEGER.
|
||||
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
|
||||
@@ LUA_INTEGER_FMT is the format for writing integers.
|
||||
@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
|
||||
|
||||
184
lundump.c
184
lundump.c
@@ -26,7 +26,7 @@
|
||||
|
||||
|
||||
#if !defined(luai_verifycode)
|
||||
#define luai_verifycode(L,b,f) /* empty */
|
||||
#define luai_verifycode(L,f) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
@@ -44,21 +44,21 @@ static l_noret error (LoadState *S, const char *why) {
|
||||
|
||||
|
||||
/*
|
||||
** All high-level loads go through LoadVector; you can change it to
|
||||
** All high-level loads go through loadVector; you can change it to
|
||||
** adapt to the endianness of the input
|
||||
*/
|
||||
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
|
||||
#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0]))
|
||||
|
||||
static void LoadBlock (LoadState *S, void *b, size_t size) {
|
||||
static void loadBlock (LoadState *S, void *b, size_t size) {
|
||||
if (luaZ_read(S->Z, b, size) != 0)
|
||||
error(S, "truncated chunk");
|
||||
}
|
||||
|
||||
|
||||
#define LoadVar(S,x) LoadVector(S,&x,1)
|
||||
#define loadVar(S,x) loadVector(S,&x,1)
|
||||
|
||||
|
||||
static lu_byte LoadByte (LoadState *S) {
|
||||
static lu_byte loadByte (LoadState *S) {
|
||||
int b = zgetc(S->Z);
|
||||
if (b == EOZ)
|
||||
error(S, "truncated chunk");
|
||||
@@ -66,12 +66,12 @@ static lu_byte LoadByte (LoadState *S) {
|
||||
}
|
||||
|
||||
|
||||
static size_t LoadUnsigned (LoadState *S, size_t limit) {
|
||||
static size_t loadUnsigned (LoadState *S, size_t limit) {
|
||||
size_t x = 0;
|
||||
int b;
|
||||
limit >>= 7;
|
||||
do {
|
||||
b = LoadByte(S);
|
||||
b = loadByte(S);
|
||||
if (x >= limit)
|
||||
error(S, "integer overflow");
|
||||
x = (x << 7) | (b & 0x7f);
|
||||
@@ -80,98 +80,104 @@ static size_t LoadUnsigned (LoadState *S, size_t limit) {
|
||||
}
|
||||
|
||||
|
||||
static size_t LoadSize (LoadState *S) {
|
||||
return LoadUnsigned(S, ~(size_t)0);
|
||||
static size_t loadSize (LoadState *S) {
|
||||
return loadUnsigned(S, ~(size_t)0);
|
||||
}
|
||||
|
||||
|
||||
static int LoadInt (LoadState *S) {
|
||||
return cast_int(LoadUnsigned(S, INT_MAX));
|
||||
static int loadInt (LoadState *S) {
|
||||
return cast_int(loadUnsigned(S, INT_MAX));
|
||||
}
|
||||
|
||||
|
||||
static lua_Number LoadNumber (LoadState *S) {
|
||||
static lua_Number loadNumber (LoadState *S) {
|
||||
lua_Number x;
|
||||
LoadVar(S, x);
|
||||
loadVar(S, x);
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
static lua_Integer LoadInteger (LoadState *S) {
|
||||
static lua_Integer loadInteger (LoadState *S) {
|
||||
lua_Integer x;
|
||||
LoadVar(S, x);
|
||||
loadVar(S, x);
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Load a nullable string
|
||||
** Load a nullable string into prototype 'p'.
|
||||
*/
|
||||
static TString *LoadStringN (LoadState *S) {
|
||||
size_t size = LoadSize(S);
|
||||
if (size == 0)
|
||||
static TString *loadStringN (LoadState *S, Proto *p) {
|
||||
lua_State *L = S->L;
|
||||
TString *ts;
|
||||
size_t size = loadSize(S);
|
||||
if (size == 0) /* no string? */
|
||||
return NULL;
|
||||
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
||||
char buff[LUAI_MAXSHORTLEN];
|
||||
LoadVector(S, buff, size);
|
||||
return luaS_newlstr(S->L, buff, size);
|
||||
loadVector(S, buff, size); /* load string into buffer */
|
||||
ts = luaS_newlstr(L, buff, size); /* create string */
|
||||
}
|
||||
else { /* long string */
|
||||
TString *ts = luaS_createlngstrobj(S->L, size);
|
||||
LoadVector(S, getstr(ts), size); /* load directly in final place */
|
||||
return ts;
|
||||
ts = luaS_createlngstrobj(L, size); /* create string */
|
||||
loadVector(S, getstr(ts), size); /* load directly in final place */
|
||||
}
|
||||
luaC_objbarrier(L, p, ts);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Load a non-nullable string.
|
||||
** Load a non-nullable string into prototype 'p'.
|
||||
*/
|
||||
static TString *LoadString (LoadState *S) {
|
||||
TString *st = LoadStringN(S);
|
||||
static TString *loadString (LoadState *S, Proto *p) {
|
||||
TString *st = loadStringN(S, p);
|
||||
if (st == NULL)
|
||||
error(S, "bad format for constant string");
|
||||
return st;
|
||||
}
|
||||
|
||||
|
||||
static void LoadCode (LoadState *S, Proto *f) {
|
||||
int n = LoadInt(S);
|
||||
static void loadCode (LoadState *S, Proto *f) {
|
||||
int n = loadInt(S);
|
||||
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
||||
f->sizecode = n;
|
||||
LoadVector(S, f->code, n);
|
||||
loadVector(S, f->code, n);
|
||||
}
|
||||
|
||||
|
||||
static void LoadFunction(LoadState *S, Proto *f, TString *psource);
|
||||
static void loadFunction(LoadState *S, Proto *f, TString *psource);
|
||||
|
||||
|
||||
static void LoadConstants (LoadState *S, Proto *f) {
|
||||
static void loadConstants (LoadState *S, Proto *f) {
|
||||
int i;
|
||||
int n = LoadInt(S);
|
||||
int n = loadInt(S);
|
||||
f->k = luaM_newvectorchecked(S->L, n, TValue);
|
||||
f->sizek = n;
|
||||
for (i = 0; i < n; i++)
|
||||
setnilvalue(&f->k[i]);
|
||||
for (i = 0; i < n; i++) {
|
||||
TValue *o = &f->k[i];
|
||||
int t = LoadByte(S);
|
||||
int t = loadByte(S);
|
||||
switch (t) {
|
||||
case LUA_TNIL:
|
||||
case LUA_VNIL:
|
||||
setnilvalue(o);
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
setbvalue(o, LoadByte(S));
|
||||
case LUA_VFALSE:
|
||||
setbfvalue(o);
|
||||
break;
|
||||
case LUA_TNUMFLT:
|
||||
setfltvalue(o, LoadNumber(S));
|
||||
case LUA_VTRUE:
|
||||
setbtvalue(o);
|
||||
break;
|
||||
case LUA_TNUMINT:
|
||||
setivalue(o, LoadInteger(S));
|
||||
case LUA_VNUMFLT:
|
||||
setfltvalue(o, loadNumber(S));
|
||||
break;
|
||||
case LUA_TSHRSTR:
|
||||
case LUA_TLNGSTR:
|
||||
setsvalue2n(S->L, o, LoadString(S));
|
||||
case LUA_VNUMINT:
|
||||
setivalue(o, loadInteger(S));
|
||||
break;
|
||||
case LUA_VSHRSTR:
|
||||
case LUA_VLNGSTR:
|
||||
setsvalue2n(S->L, o, loadString(S, f));
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
}
|
||||
@@ -179,91 +185,92 @@ static void LoadConstants (LoadState *S, Proto *f) {
|
||||
}
|
||||
|
||||
|
||||
static void LoadProtos (LoadState *S, Proto *f) {
|
||||
static void loadProtos (LoadState *S, Proto *f) {
|
||||
int i;
|
||||
int n = LoadInt(S);
|
||||
int n = loadInt(S);
|
||||
f->p = luaM_newvectorchecked(S->L, n, Proto *);
|
||||
f->sizep = n;
|
||||
for (i = 0; i < n; i++)
|
||||
f->p[i] = NULL;
|
||||
for (i = 0; i < n; i++) {
|
||||
f->p[i] = luaF_newproto(S->L);
|
||||
LoadFunction(S, f->p[i], f->source);
|
||||
luaC_objbarrier(S->L, f, f->p[i]);
|
||||
loadFunction(S, f->p[i], f->source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void LoadUpvalues (LoadState *S, Proto *f) {
|
||||
static void loadUpvalues (LoadState *S, Proto *f) {
|
||||
int i, n;
|
||||
n = LoadInt(S);
|
||||
n = loadInt(S);
|
||||
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
|
||||
f->sizeupvalues = n;
|
||||
for (i = 0; i < n; i++)
|
||||
f->upvalues[i].name = NULL;
|
||||
for (i = 0; i < n; i++) {
|
||||
f->upvalues[i].instack = LoadByte(S);
|
||||
f->upvalues[i].idx = LoadByte(S);
|
||||
f->upvalues[i].name = NULL;
|
||||
f->upvalues[i].instack = loadByte(S);
|
||||
f->upvalues[i].idx = loadByte(S);
|
||||
f->upvalues[i].kind = loadByte(S);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void LoadDebug (LoadState *S, Proto *f) {
|
||||
static void loadDebug (LoadState *S, Proto *f) {
|
||||
int i, n;
|
||||
n = LoadInt(S);
|
||||
n = loadInt(S);
|
||||
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
||||
f->sizelineinfo = n;
|
||||
LoadVector(S, f->lineinfo, n);
|
||||
n = LoadInt(S);
|
||||
loadVector(S, f->lineinfo, n);
|
||||
n = loadInt(S);
|
||||
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
||||
f->sizeabslineinfo = n;
|
||||
for (i = 0; i < n; i++) {
|
||||
f->abslineinfo[i].pc = LoadInt(S);
|
||||
f->abslineinfo[i].line = LoadInt(S);
|
||||
f->abslineinfo[i].pc = loadInt(S);
|
||||
f->abslineinfo[i].line = loadInt(S);
|
||||
}
|
||||
n = LoadInt(S);
|
||||
n = loadInt(S);
|
||||
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
|
||||
f->sizelocvars = n;
|
||||
for (i = 0; i < n; i++)
|
||||
f->locvars[i].varname = NULL;
|
||||
for (i = 0; i < n; i++) {
|
||||
f->locvars[i].varname = LoadStringN(S);
|
||||
f->locvars[i].startpc = LoadInt(S);
|
||||
f->locvars[i].endpc = LoadInt(S);
|
||||
f->locvars[i].varname = loadStringN(S, f);
|
||||
f->locvars[i].startpc = loadInt(S);
|
||||
f->locvars[i].endpc = loadInt(S);
|
||||
}
|
||||
n = LoadInt(S);
|
||||
n = loadInt(S);
|
||||
for (i = 0; i < n; i++)
|
||||
f->upvalues[i].name = LoadStringN(S);
|
||||
f->upvalues[i].name = loadStringN(S, f);
|
||||
}
|
||||
|
||||
|
||||
static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
|
||||
f->source = LoadStringN(S);
|
||||
static void loadFunction (LoadState *S, Proto *f, TString *psource) {
|
||||
f->source = loadStringN(S, f);
|
||||
if (f->source == NULL) /* no source in dump? */
|
||||
f->source = psource; /* reuse parent's source */
|
||||
f->linedefined = LoadInt(S);
|
||||
f->lastlinedefined = LoadInt(S);
|
||||
f->numparams = LoadByte(S);
|
||||
f->is_vararg = LoadByte(S);
|
||||
f->maxstacksize = LoadByte(S);
|
||||
LoadCode(S, f);
|
||||
LoadConstants(S, f);
|
||||
LoadUpvalues(S, f);
|
||||
LoadProtos(S, f);
|
||||
LoadDebug(S, f);
|
||||
f->linedefined = loadInt(S);
|
||||
f->lastlinedefined = loadInt(S);
|
||||
f->numparams = loadByte(S);
|
||||
f->is_vararg = loadByte(S);
|
||||
f->maxstacksize = loadByte(S);
|
||||
loadCode(S, f);
|
||||
loadConstants(S, f);
|
||||
loadUpvalues(S, f);
|
||||
loadProtos(S, f);
|
||||
loadDebug(S, f);
|
||||
}
|
||||
|
||||
|
||||
static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
||||
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
|
||||
size_t len = strlen(s);
|
||||
LoadVector(S, buff, len);
|
||||
loadVector(S, buff, len);
|
||||
if (memcmp(s, buff, len) != 0)
|
||||
error(S, msg);
|
||||
}
|
||||
|
||||
|
||||
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
||||
if (LoadByte(S) != size)
|
||||
if (loadByte(S) != size)
|
||||
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
|
||||
}
|
||||
|
||||
@@ -273,23 +280,23 @@ static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
||||
static void checkHeader (LoadState *S) {
|
||||
/* skip 1st char (already read and checked) */
|
||||
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
|
||||
if (LoadInt(S) != LUAC_VERSION)
|
||||
if (loadByte(S) != LUAC_VERSION)
|
||||
error(S, "version mismatch");
|
||||
if (LoadByte(S) != LUAC_FORMAT)
|
||||
if (loadByte(S) != LUAC_FORMAT)
|
||||
error(S, "format mismatch");
|
||||
checkliteral(S, LUAC_DATA, "corrupted chunk");
|
||||
checksize(S, Instruction);
|
||||
checksize(S, lua_Integer);
|
||||
checksize(S, lua_Number);
|
||||
if (LoadInteger(S) != LUAC_INT)
|
||||
if (loadInteger(S) != LUAC_INT)
|
||||
error(S, "integer format mismatch");
|
||||
if (LoadNumber(S) != LUAC_NUM)
|
||||
if (loadNumber(S) != LUAC_NUM)
|
||||
error(S, "float format mismatch");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** load precompiled chunk
|
||||
** Load precompiled chunk.
|
||||
*/
|
||||
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
||||
LoadState S;
|
||||
@@ -303,13 +310,14 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
||||
S.L = L;
|
||||
S.Z = Z;
|
||||
checkHeader(&S);
|
||||
cl = luaF_newLclosure(L, LoadByte(&S));
|
||||
cl = luaF_newLclosure(L, loadByte(&S));
|
||||
setclLvalue2s(L, L->top, cl);
|
||||
luaD_inctop(L);
|
||||
cl->p = luaF_newproto(L);
|
||||
LoadFunction(&S, cl->p, NULL);
|
||||
luaC_objbarrier(L, cl, cl->p);
|
||||
loadFunction(&S, cl->p, NULL);
|
||||
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
||||
luai_verifycode(L, buff, cl->p);
|
||||
luai_verifycode(L, cl->p);
|
||||
return cl;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,12 @@
|
||||
#define LUAC_INT 0x5678
|
||||
#define LUAC_NUM cast_num(370.5)
|
||||
|
||||
#define LUAC_VERSION LUA_VERSION_NUM
|
||||
/*
|
||||
** Encode major-minor version in one byte, one nibble for each
|
||||
*/
|
||||
#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */
|
||||
#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
|
||||
|
||||
#define LUAC_FORMAT 0 /* this is the official format */
|
||||
|
||||
/* load one chunk; from lundump.c */
|
||||
|
||||
16
lutf8lib.c
16
lutf8lib.c
@@ -29,7 +29,7 @@
|
||||
** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
|
||||
*/
|
||||
#if (UINT_MAX >> 30) >= 1
|
||||
typedef unsigned int utfint;
|
||||
typedef unsigned int utfint;
|
||||
#else
|
||||
typedef unsigned long utfint;
|
||||
#endif
|
||||
@@ -97,13 +97,13 @@ static int utflen (lua_State *L) {
|
||||
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
||||
int lax = lua_toboolean(L, 4);
|
||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
||||
"initial position out of string");
|
||||
"initial position out of bounds");
|
||||
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
||||
"final position out of string");
|
||||
"final position out of bounds");
|
||||
while (posi <= posj) {
|
||||
const char *s1 = utf8_decode(s + posi, NULL, !lax);
|
||||
if (s1 == NULL) { /* conversion error? */
|
||||
lua_pushnil(L); /* return nil ... */
|
||||
luaL_pushfail(L); /* return fail ... */
|
||||
lua_pushinteger(L, posi + 1); /* ... and current position */
|
||||
return 2;
|
||||
}
|
||||
@@ -127,8 +127,8 @@ static int codepoint (lua_State *L) {
|
||||
int lax = lua_toboolean(L, 4);
|
||||
int n;
|
||||
const char *se;
|
||||
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
||||
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
||||
luaL_argcheck(L, posi >= 1, 2, "out of bounds");
|
||||
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
|
||||
if (posi > pose) return 0; /* empty interval; return no values */
|
||||
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
||||
return luaL_error(L, "string slice too long");
|
||||
@@ -187,7 +187,7 @@ static int byteoffset (lua_State *L) {
|
||||
lua_Integer posi = (n >= 0) ? 1 : len + 1;
|
||||
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
||||
"position out of range");
|
||||
"position out of bounds");
|
||||
if (n == 0) {
|
||||
/* find beginning of current byte sequence */
|
||||
while (posi > 0 && iscont(s + posi)) posi--;
|
||||
@@ -216,7 +216,7 @@ static int byteoffset (lua_State *L) {
|
||||
if (n == 0) /* did it find given character? */
|
||||
lua_pushinteger(L, posi + 1);
|
||||
else /* no such character */
|
||||
lua_pushnil(L);
|
||||
luaL_pushfail(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
19
lvm.h
19
lvm.h
@@ -33,10 +33,20 @@
|
||||
** integral values)
|
||||
*/
|
||||
#if !defined(LUA_FLOORN2I)
|
||||
#define LUA_FLOORN2I 0
|
||||
#define LUA_FLOORN2I F2Ieq
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Rounding modes for float->integer coercion
|
||||
*/
|
||||
typedef enum {
|
||||
F2Ieq, /* no rounding; accepts only integral values */
|
||||
F2Ifloor, /* takes the floor of the number */
|
||||
F2Iceil /* takes the ceil of the number */
|
||||
} F2Imod;
|
||||
|
||||
|
||||
/* convert an object to a float (including string coercion) */
|
||||
#define tonumber(o,n) \
|
||||
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
||||
@@ -104,9 +114,10 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
|
||||
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
||||
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
||||
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
||||
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
|
||||
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode);
|
||||
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode);
|
||||
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
|
||||
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
|
||||
F2Imod mode);
|
||||
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
|
||||
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
||||
StkId val, const TValue *slot);
|
||||
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
||||
|
||||
32
makefile
32
makefile
@@ -1,11 +1,11 @@
|
||||
# makefile for building Lua
|
||||
# see INSTALL for installation instructions
|
||||
# see ../Makefile and luaconf.h for further customization
|
||||
# Developer's makefile for building Lua
|
||||
# see luaconf.h for further customization
|
||||
|
||||
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
||||
|
||||
# Warnings valid for both C and C++
|
||||
CWARNSCPP= \
|
||||
-fmax-errors=5 \
|
||||
-Wextra \
|
||||
-Wshadow \
|
||||
-Wsign-compare \
|
||||
@@ -14,14 +14,13 @@ CWARNSCPP= \
|
||||
-Wredundant-decls \
|
||||
-Wdisabled-optimization \
|
||||
-Wdouble-promotion \
|
||||
#-Wno-aggressive-loop-optimizations \
|
||||
#-Wlogical-op \
|
||||
#-Wfatal-errors \
|
||||
#-Wstrict-aliasing=3 \
|
||||
-Wlogical-op \
|
||||
-Wno-aggressive-loop-optimizations \
|
||||
# the next warnings might be useful sometimes,
|
||||
# but usually they generate too much noise
|
||||
# -Werror \
|
||||
# -pedantic # warns if we use jump tables \
|
||||
# the next warnings generate too much noise, so they are disabled
|
||||
# -Wconversion -Wno-sign-conversion \
|
||||
# -Wconversion \
|
||||
# -Wsign-conversion \
|
||||
# -Wstrict-overflow=2 \
|
||||
# -Wformat=2 \
|
||||
@@ -46,10 +45,10 @@ CWARNS= $(CWARNSCPP) $(CWARNSC)
|
||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||
# ('-ftrapv' for runtime checks of integer overflows)
|
||||
# -fsanitize=undefined -ftrapv -fno-inline
|
||||
TESTS= -DLUA_USER_H='"ltests.h"' -O0
|
||||
# TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g
|
||||
|
||||
|
||||
# LOCAL = $(TESTS) $(CWARNS) -g
|
||||
LOCAL = $(TESTS) $(CWARNS)
|
||||
|
||||
|
||||
# enable Linux goodies
|
||||
@@ -106,8 +105,17 @@ $(LUA_T): $(LUA_O) $(CORE_T)
|
||||
$(LUAC_T): $(LUAC_O) $(CORE_T)
|
||||
$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS)
|
||||
|
||||
llex.o:
|
||||
$(CC) $(CFLAGS) -Os -c llex.c
|
||||
|
||||
lparser.o:
|
||||
$(CC) $(CFLAGS) -Os -c lparser.c
|
||||
|
||||
lcode.o:
|
||||
$(CC) $(CFLAGS) -Os -c lcode.c
|
||||
|
||||
|
||||
clean:
|
||||
rcsclean -u
|
||||
$(RM) $(ALL_T) $(ALL_O)
|
||||
|
||||
depend:
|
||||
|
||||
@@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
|
||||
<p>
|
||||
<small>
|
||||
<a href="http://www.lua.org/copyright.html">Copyright</a>
|
||||
© 2019 Lua.org, PUC-Rio. All rights reserved.
|
||||
© 2020 Lua.org, PUC-Rio. All rights reserved.
|
||||
</small>
|
||||
<hr>
|
||||
|
||||
@@ -324,6 +324,7 @@ N = function (s) return (string.gsub(s, " ", " ")) end,
|
||||
NE = id, -- tag"foreignphrase",
|
||||
num = id,
|
||||
["nil"] = fixed(Tag.b"nil"),
|
||||
fail = fixed(Tag.b"fail"),
|
||||
Open = fixed"{",
|
||||
part = section("h1", true),
|
||||
Pat = compose(verbfixed, prepos("'", "'")),
|
||||
|
||||
906
manual/manual.of
906
manual/manual.of
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,8 @@
|
||||
|
||||
local version = "Lua 5.4"
|
||||
if _VERSION ~= version then
|
||||
warn("This test suite is for ", version,
|
||||
", not for ", _VERSION, "\nExiting tests")
|
||||
io.stderr:write("This test suite is for ", version,
|
||||
", not for ", _VERSION, "\nExiting tests")
|
||||
return
|
||||
end
|
||||
|
||||
@@ -37,8 +37,6 @@ end
|
||||
-- tests should require debug when needed
|
||||
debug = nil
|
||||
|
||||
require"bwcoercion"
|
||||
|
||||
|
||||
if usertests then
|
||||
T = nil -- no "internal" tests for user tests
|
||||
@@ -46,7 +44,6 @@ else
|
||||
T = rawget(_G, "T") -- avoid problems with 'strict' module
|
||||
end
|
||||
|
||||
math.randomseed(0)
|
||||
|
||||
--[=[
|
||||
example of a long [comment],
|
||||
@@ -54,6 +51,14 @@ math.randomseed(0)
|
||||
|
||||
]=]
|
||||
|
||||
print("\n\tStarting Tests")
|
||||
|
||||
do
|
||||
-- set random seed
|
||||
local random_x, random_y = math.randomseed()
|
||||
print(string.format("random seeds: %d, %d", random_x, random_y))
|
||||
end
|
||||
|
||||
print("current path:\n****" .. package.path .. "****\n")
|
||||
|
||||
|
||||
@@ -95,6 +100,8 @@ local function F (m)
|
||||
end
|
||||
end
|
||||
|
||||
local Cstacklevel
|
||||
|
||||
local showmem
|
||||
if not T then
|
||||
local max = 0
|
||||
@@ -104,6 +111,7 @@ if not T then
|
||||
print(format(" ---- total memory: %s, max memory: %s ----\n",
|
||||
F(m), F(max)))
|
||||
end
|
||||
Cstacklevel = function () return 0 end -- no info about stack level
|
||||
else
|
||||
showmem = function ()
|
||||
T.checkmemory()
|
||||
@@ -117,9 +125,16 @@ else
|
||||
T.totalmem"string", T.totalmem"table", T.totalmem"function",
|
||||
T.totalmem"userdata", T.totalmem"thread"))
|
||||
end
|
||||
|
||||
Cstacklevel = function ()
|
||||
local _, _, ncalls, nci = T.stacklevel()
|
||||
return ncalls + nci -- number of free slots in the C stack
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local Cstack = Cstacklevel()
|
||||
|
||||
--
|
||||
-- redefine dofile to run files through dump/undump
|
||||
--
|
||||
@@ -195,7 +210,12 @@ if #msgs > 0 then
|
||||
end
|
||||
|
||||
print("(there should be two warnings now)")
|
||||
warn("@on")
|
||||
warn("#This is ", "an expected", " warning")
|
||||
warn("@off")
|
||||
warn("******** THIS WARNING SHOULD NOT APPEAR **********")
|
||||
warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********")
|
||||
warn("@on")
|
||||
warn("#This is", " another one")
|
||||
|
||||
-- no test module should define 'debug'
|
||||
@@ -211,6 +231,10 @@ debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
|
||||
-- to survive outside block
|
||||
_G.showmem = showmem
|
||||
|
||||
|
||||
assert(Cstack == Cstacklevel(),
|
||||
"should be at the same C-stack level it was when started the tests")
|
||||
|
||||
end --)
|
||||
|
||||
local _G, showmem, print, format, clock, time, difftime,
|
||||
|
||||
@@ -241,6 +241,23 @@ assert(a == 20 and b == false)
|
||||
a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
|
||||
assert(a == 20 and b == true)
|
||||
|
||||
|
||||
do -- testing lessthan and lessequal with metamethods
|
||||
local mt = {__lt = function (a,b) return a[1] < b[1] end,
|
||||
__le = function (a,b) return a[1] <= b[1] end,
|
||||
__eq = function (a,b) return a[1] == b[1] end}
|
||||
local function O (x)
|
||||
return setmetatable({x}, mt)
|
||||
end
|
||||
|
||||
local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2))
|
||||
assert(a == true and b == 10)
|
||||
local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2))
|
||||
assert(a == false and b == 10)
|
||||
local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3))
|
||||
assert(a == true and b == 10)
|
||||
end
|
||||
|
||||
-- testing length
|
||||
local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
|
||||
a,b,c = T.testC([[
|
||||
@@ -354,8 +371,11 @@ assert(to("topointer", nil) == null)
|
||||
assert(to("topointer", "abc") ~= null)
|
||||
assert(to("topointer", string.rep("x", 10)) ==
|
||||
to("topointer", string.rep("x", 10))) -- short strings
|
||||
assert(to("topointer", string.rep("x", 300)) ~=
|
||||
to("topointer", string.rep("x", 300))) -- long strings
|
||||
do -- long strings
|
||||
local s1 = string.rep("x", 300)
|
||||
local s2 = string.rep("x", 300)
|
||||
assert(to("topointer", s1) ~= to("topointer", s2))
|
||||
end
|
||||
assert(to("topointer", T.pushuserdata(20)) ~= null)
|
||||
assert(to("topointer", io.read) ~= null) -- light C function
|
||||
assert(to("topointer", hfunc) ~= null) -- "heavy" C function
|
||||
@@ -496,9 +516,57 @@ print"+"
|
||||
|
||||
do -- getp/setp
|
||||
local a = {}
|
||||
T.testC("rawsetp 2 1", a, 20)
|
||||
local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
|
||||
assert(a == a1)
|
||||
assert(a[T.pushuserdata(1)] == 20)
|
||||
assert(T.testC("rawgetp 2 1; return 1", a) == 20)
|
||||
local a1, res = T.testC("rawgetp -1 1; return 2", a)
|
||||
assert(a == a1 and res == 20)
|
||||
end
|
||||
|
||||
|
||||
do -- using the table itself as index
|
||||
local a = {}
|
||||
a[a] = 10
|
||||
local prog = "gettable -1; return *"
|
||||
local res = {T.testC(prog, a)}
|
||||
assert(#res == 2 and res[1] == prog and res[2] == 10)
|
||||
|
||||
local prog = "settable -2; return *"
|
||||
local res = {T.testC(prog, a, 20)}
|
||||
assert(a[a] == 20)
|
||||
assert(#res == 1 and res[1] == prog)
|
||||
|
||||
-- raw
|
||||
a[a] = 10
|
||||
local prog = "rawget -1; return *"
|
||||
local res = {T.testC(prog, a)}
|
||||
assert(#res == 2 and res[1] == prog and res[2] == 10)
|
||||
|
||||
local prog = "rawset -2; return *"
|
||||
local res = {T.testC(prog, a, 20)}
|
||||
assert(a[a] == 20)
|
||||
assert(#res == 1 and res[1] == prog)
|
||||
|
||||
-- using the table as the value to set
|
||||
local prog = "rawset -1; return *"
|
||||
local res = {T.testC(prog, 30, a)}
|
||||
assert(a[30] == a)
|
||||
assert(#res == 1 and res[1] == prog)
|
||||
|
||||
local prog = "settable -1; return *"
|
||||
local res = {T.testC(prog, 40, a)}
|
||||
assert(a[40] == a)
|
||||
assert(#res == 1 and res[1] == prog)
|
||||
|
||||
local prog = "rawseti -1 100; return *"
|
||||
local res = {T.testC(prog, a)}
|
||||
assert(a[100] == a)
|
||||
assert(#res == 1 and res[1] == prog)
|
||||
|
||||
local prog = "seti -1 200; return *"
|
||||
local res = {T.testC(prog, a)}
|
||||
assert(a[200] == a)
|
||||
assert(#res == 1 and res[1] == prog)
|
||||
end
|
||||
|
||||
a = {x=0, y=12}
|
||||
@@ -632,7 +700,7 @@ for k, v in ipairs(t) do
|
||||
assert(v1 == v and p)
|
||||
end
|
||||
|
||||
assert(debug.getuservalue(4) == nil)
|
||||
assert(not debug.getuservalue(4))
|
||||
|
||||
debug.setuservalue(b, function () return 10 end, 10)
|
||||
collectgarbage() -- function should not be collected
|
||||
@@ -737,7 +805,7 @@ F = function (x)
|
||||
if A ~= nil then
|
||||
assert(type(A) == "userdata")
|
||||
assert(T.udataval(A) == B)
|
||||
debug.getmetatable(A) -- just acess it
|
||||
debug.getmetatable(A) -- just access it
|
||||
end
|
||||
A = x -- ressucita userdata
|
||||
B = udval
|
||||
@@ -911,6 +979,7 @@ assert(t[7] == nil)
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
do -- testing errors during GC
|
||||
warn("@off")
|
||||
collectgarbage("stop")
|
||||
local a = {}
|
||||
for i=1,20 do
|
||||
@@ -928,6 +997,7 @@ do -- testing errors during GC
|
||||
collectgarbage()
|
||||
assert(A == 10) -- number of normal collections
|
||||
collectgarbage("restart")
|
||||
warn("@on")
|
||||
end
|
||||
-------------------------------------------------------------------------
|
||||
-- test for userdata vals
|
||||
@@ -1030,7 +1100,7 @@ do
|
||||
assert(type(a[1]) == "string" and a[2][1] == 11)
|
||||
assert(#openresource == 0) -- was closed
|
||||
|
||||
-- error
|
||||
-- closing by error
|
||||
local a, b = pcall(T.makeCfunc[[
|
||||
call 0 1 # create resource
|
||||
toclose -1 # mark it to be closed
|
||||
@@ -1039,6 +1109,15 @@ do
|
||||
assert(a == false and b[1] == 11)
|
||||
assert(#openresource == 0) -- was closed
|
||||
|
||||
-- non-closable value
|
||||
local a, b = pcall(T.makeCfunc[[
|
||||
newtable # create non-closable object
|
||||
toclose -1 # mark it to be closed (should raise an error)
|
||||
abort # will not be executed
|
||||
]])
|
||||
assert(a == false and
|
||||
string.find(b, "non%-closable value"))
|
||||
|
||||
local function check (n)
|
||||
assert(#openresource == n)
|
||||
end
|
||||
@@ -1137,7 +1216,7 @@ end)
|
||||
testamem("to-be-closed variables", function()
|
||||
local flag
|
||||
do
|
||||
local <toclose> x =
|
||||
local x <close> =
|
||||
setmetatable({}, {__close = function () flag = true end})
|
||||
flag = false
|
||||
local x = {}
|
||||
|
||||
@@ -28,7 +28,7 @@ do
|
||||
local path = table.concat(t, ";")
|
||||
-- use that path in a search
|
||||
local s, err = package.searchpath("xuxu", path)
|
||||
-- search fails; check that message has an occurence of
|
||||
-- search fails; check that message has an occurrence of
|
||||
-- '??????????' with ? replaced by xuxu and at least 'max' lines
|
||||
assert(not s and
|
||||
string.find(err, string.rep("xuxu", 10)) and
|
||||
@@ -47,6 +47,29 @@ do
|
||||
package.path = oldpath
|
||||
end
|
||||
|
||||
|
||||
do print"testing 'require' message"
|
||||
local oldpath = package.path
|
||||
local oldcpath = package.cpath
|
||||
|
||||
package.path = "?.lua;?/?"
|
||||
package.cpath = "?.so;?/init"
|
||||
|
||||
local st, msg = pcall(require, 'XXX')
|
||||
|
||||
local expected = [[module 'XXX' not found:
|
||||
no field package.preload['XXX']
|
||||
no file 'XXX.lua'
|
||||
no file 'XXX/XXX'
|
||||
no file 'XXX.so'
|
||||
no file 'XXX/init']]
|
||||
|
||||
assert(msg == expected)
|
||||
|
||||
package.path = oldpath
|
||||
package.cpath = oldcpath
|
||||
end
|
||||
|
||||
print('+')
|
||||
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size
|
||||
|
||||
local longs = string.rep("\0", ssize) -- create one long string
|
||||
|
||||
-- create function to concatentate 'repstrings' copies of its argument
|
||||
-- create function to concatenate 'repstrings' copies of its argument
|
||||
local rep = assert(load(
|
||||
"local a = ...; return " .. string.rep("a", repstrings, "..")))
|
||||
|
||||
|
||||
@@ -60,9 +60,9 @@ assert("1234.0" << "5.0" == 1234 * 32)
|
||||
assert("0xffff.0" ~ "0xAAAA" == 0x5555)
|
||||
assert(~"0x0.000p4" == -1)
|
||||
|
||||
assert("7" .. 3 << 1 == 146)
|
||||
assert(10 >> 1 .. "9" == 0)
|
||||
assert(10 | 1 .. "9" == 27)
|
||||
assert(("7" .. 3) << 1 == 146)
|
||||
assert(0xffffffff >> (1 .. "9") == 0x1fff)
|
||||
assert(10 | (1 .. "9") == 27)
|
||||
|
||||
do
|
||||
local st, msg = pcall(function () return 4 & "a" end)
|
||||
|
||||
@@ -107,7 +107,9 @@ end
|
||||
deep(10)
|
||||
deep(180)
|
||||
|
||||
-- testing tail calls
|
||||
|
||||
print"testing tail calls"
|
||||
|
||||
function deep (n) if n>0 then return deep(n-1) else return 101 end end
|
||||
assert(deep(30000) == 101)
|
||||
a = {}
|
||||
@@ -148,9 +150,47 @@ do -- tail calls x varargs
|
||||
assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
|
||||
end
|
||||
|
||||
|
||||
|
||||
do -- tail calls x chain of __call
|
||||
local n = 10000 -- depth
|
||||
|
||||
local function foo ()
|
||||
if n == 0 then return 1023
|
||||
else n = n - 1; return foo()
|
||||
end
|
||||
end
|
||||
|
||||
-- build a chain of __call metamethods ending in function 'foo'
|
||||
for i = 1, 100 do
|
||||
foo = setmetatable({}, {__call = foo})
|
||||
end
|
||||
|
||||
-- call the first one as a tail call in a new coroutine
|
||||
-- (to ensure stack is not preallocated)
|
||||
assert(coroutine.wrap(function() return foo() end)() == 1023)
|
||||
end
|
||||
|
||||
print('+')
|
||||
|
||||
|
||||
do -- testing chains of '__call'
|
||||
local N = 20
|
||||
local u = table.pack
|
||||
for i = 1, N do
|
||||
u = setmetatable({i}, {__call = u})
|
||||
end
|
||||
|
||||
local Res = u("a", "b", "c")
|
||||
|
||||
assert(Res.n == N + 3)
|
||||
for i = 1, N do
|
||||
assert(Res[i][1] == i)
|
||||
end
|
||||
assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
|
||||
end
|
||||
|
||||
|
||||
a = nil
|
||||
(function (x) a=x end)(23)
|
||||
assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
||||
@@ -382,9 +422,9 @@ assert((function (a) return a end)() == nil)
|
||||
|
||||
print("testing binary chunks")
|
||||
do
|
||||
local header = string.pack("c4BBBc6BBBj",
|
||||
local header = string.pack("c4BBc6BBBj",
|
||||
"\27Lua", -- signature
|
||||
(504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504)
|
||||
0x54, -- version 5.4 (0x54)
|
||||
0, -- format
|
||||
"\x19\x93\r\n\x1a\n", -- data
|
||||
4, -- size of instruction
|
||||
|
||||
189
testes/code.lua
189
testes/code.lua
@@ -7,6 +7,23 @@ if T==nil then
|
||||
end
|
||||
print "testing code generation and optimizations"
|
||||
|
||||
-- to test constant propagation
|
||||
local k0aux <const> = 0
|
||||
local k0 <const> = k0aux
|
||||
local k1 <const> = 1
|
||||
local k3 <const> = 3
|
||||
local k6 <const> = k3 + (k3 << k0)
|
||||
local kFF0 <const> = 0xFF0
|
||||
local k3_78 <const> = 3.78
|
||||
local x, k3_78_4 <const> = 10, k3_78 / 4
|
||||
assert(x == 10)
|
||||
|
||||
local kx <const> = "x"
|
||||
|
||||
local kTrue <const> = true
|
||||
local kFalse <const> = false
|
||||
|
||||
local kNil <const> = nil
|
||||
|
||||
-- this code gave an error for the code checker
|
||||
do
|
||||
@@ -27,12 +44,12 @@ end
|
||||
|
||||
local function foo ()
|
||||
local a
|
||||
a = 3;
|
||||
a = k3;
|
||||
a = 0; a = 0.0; a = -7 + 7
|
||||
a = 3.78/4; a = 3.78/4
|
||||
a = -3.78/4; a = 3.78/4; a = -3.78/4
|
||||
a = k3_78/4; a = k3_78_4
|
||||
a = -k3_78/4; a = k3_78/4; a = -3.78/4
|
||||
a = -3.79/4; a = 0.0; a = -0;
|
||||
a = 3; a = 3.0; a = 3; a = 3.0
|
||||
a = k3; a = 3.0; a = 3; a = 3.0
|
||||
end
|
||||
|
||||
checkKlist(foo, {3.78/4, -3.78/4, -3.79/4})
|
||||
@@ -77,19 +94,22 @@ end
|
||||
-- some basic instructions
|
||||
check(function () -- function does not create upvalues
|
||||
(function () end){f()}
|
||||
end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN0')
|
||||
end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
|
||||
'SETLIST', 'CALL', 'RETURN0')
|
||||
|
||||
check(function (x) -- function creates upvalues
|
||||
(function () return x end){f()}
|
||||
end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN')
|
||||
end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
|
||||
'SETLIST', 'CALL', 'RETURN')
|
||||
|
||||
|
||||
-- sequence of LOADNILs
|
||||
check(function ()
|
||||
local kNil <const> = nil
|
||||
local a,b,c
|
||||
local d; local e;
|
||||
local f,g,h;
|
||||
d = nil; d=nil; b=nil; a=nil; c=nil;
|
||||
d = nil; d=nil; b=nil; a=kNil; c=nil;
|
||||
end, 'LOADNIL', 'RETURN0')
|
||||
|
||||
check(function ()
|
||||
@@ -109,7 +129,7 @@ check (function (a,b,c) return a end, 'RETURN1')
|
||||
|
||||
|
||||
-- infinite loops
|
||||
check(function () while true do local a = -1 end end,
|
||||
check(function () while kTrue do local a = -1 end end,
|
||||
'LOADI', 'JMP', 'RETURN0')
|
||||
|
||||
check(function () while 1 do local a = -1 end end,
|
||||
@@ -124,10 +144,10 @@ check(function (a,b,c,d) return a..b..c..d end,
|
||||
'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1')
|
||||
|
||||
-- not
|
||||
check(function () return not not nil end, 'LOADBOOL', 'RETURN1')
|
||||
check(function () return not not false end, 'LOADBOOL', 'RETURN1')
|
||||
check(function () return not not true end, 'LOADBOOL', 'RETURN1')
|
||||
check(function () return not not 1 end, 'LOADBOOL', 'RETURN1')
|
||||
check(function () return not not nil end, 'LOADFALSE', 'RETURN1')
|
||||
check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1')
|
||||
check(function () return not not true end, 'LOADTRUE', 'RETURN1')
|
||||
check(function () return not not k3 end, 'LOADTRUE', 'RETURN1')
|
||||
|
||||
-- direct access to locals
|
||||
check(function ()
|
||||
@@ -136,15 +156,16 @@ check(function ()
|
||||
c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b
|
||||
end,
|
||||
'LOADNIL',
|
||||
'MUL',
|
||||
'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETFIELD', 'POW',
|
||||
'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0')
|
||||
'MUL', 'MMBIN',
|
||||
'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN',
|
||||
'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0')
|
||||
|
||||
|
||||
-- direct access to constants
|
||||
check(function ()
|
||||
local a,b
|
||||
a.x = 3.2
|
||||
local c = kNil
|
||||
a[kx] = 3.2
|
||||
a.x = b
|
||||
a[b] = 'x'
|
||||
end,
|
||||
@@ -152,8 +173,9 @@ end,
|
||||
|
||||
-- "get/set table" with numeric indices
|
||||
check(function (a)
|
||||
local k255 <const> = 255
|
||||
a[1] = a[100]
|
||||
a[255] = a[256]
|
||||
a[k255] = a[256]
|
||||
a[256] = 5
|
||||
end,
|
||||
'GETI', 'SETI',
|
||||
@@ -166,13 +188,13 @@ check(function ()
|
||||
b = a/a
|
||||
b = 5-4
|
||||
end,
|
||||
'LOADNIL', 'SUB', 'DIV', 'LOADI', 'RETURN0')
|
||||
'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0')
|
||||
|
||||
check(function ()
|
||||
local a,b
|
||||
a[true] = false
|
||||
a[kTrue] = false
|
||||
end,
|
||||
'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0')
|
||||
'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0')
|
||||
|
||||
|
||||
-- equalities
|
||||
@@ -238,68 +260,81 @@ local function checkF (func, val)
|
||||
end
|
||||
|
||||
checkF(function () return 0.0 end, 0.0)
|
||||
checkI(function () return 0 end, 0)
|
||||
checkI(function () return -0//1 end, 0)
|
||||
checkI(function () return k0 end, 0)
|
||||
checkI(function () return -k0//1 end, 0)
|
||||
checkK(function () return 3^-1 end, 1/3)
|
||||
checkK(function () return (1 + 1)^(50 + 50) end, 2^100)
|
||||
checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0)
|
||||
checkF(function () return (-3^0 + 5) // 3.0 end, 1.0)
|
||||
checkI(function () return -3 % 5 end, 2)
|
||||
checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0)
|
||||
checkI(function () return -k3 % 5 end, 2)
|
||||
checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0)
|
||||
checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0)
|
||||
checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4)
|
||||
checkI(function () return ~(~0xFF0 | 0xFF0) end, 0)
|
||||
checkI(function () return ~(~kFF0 | kFF0) end, 0)
|
||||
checkI(function () return ~~-1024.0 end, -1024)
|
||||
checkI(function () return ((100 << 6) << -4) >> 2 end, 100)
|
||||
checkI(function () return ((100 << k6) << -4) >> 2 end, 100)
|
||||
|
||||
-- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535)
|
||||
local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding
|
||||
checkI(function () return 65535 end, sbx)
|
||||
checkI(function () return -65535 end, -sbx)
|
||||
checkI(function () return 65536 end, sbx + 1)
|
||||
checkK(function () return 65537 end, sbx + 2)
|
||||
checkK(function () return -65536 end, -(sbx + 1))
|
||||
local border <const> = 65535
|
||||
checkI(function () return border end, sbx)
|
||||
checkI(function () return -border end, -sbx)
|
||||
checkI(function () return border + 1 end, sbx + 1)
|
||||
checkK(function () return border + 2 end, sbx + 2)
|
||||
checkK(function () return -(border + 1) end, -(sbx + 1))
|
||||
|
||||
checkF(function () return 65535.0 end, sbx + 0.0)
|
||||
checkF(function () return -65535.0 end, -sbx + 0.0)
|
||||
checkF(function () return 65536.0 end, (sbx + 1.0))
|
||||
checkK(function () return 65537.0 end, (sbx + 2.0))
|
||||
checkK(function () return -65536.0 end, -(sbx + 1.0))
|
||||
local border <const> = 65535.0
|
||||
checkF(function () return border end, sbx + 0.0)
|
||||
checkF(function () return -border end, -sbx + 0.0)
|
||||
checkF(function () return border + 1 end, (sbx + 1.0))
|
||||
checkK(function () return border + 2 end, (sbx + 2.0))
|
||||
checkK(function () return -(border + 1) end, -(sbx + 1.0))
|
||||
|
||||
|
||||
-- immediate operands
|
||||
checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1')
|
||||
checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1')
|
||||
checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1')
|
||||
checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1')
|
||||
checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1')
|
||||
checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1')
|
||||
checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1')
|
||||
checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1')
|
||||
checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1')
|
||||
checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1')
|
||||
checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1')
|
||||
checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1')
|
||||
checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1')
|
||||
checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1')
|
||||
checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return 128 + x end, 0.0, 128.0,
|
||||
'ADDI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x * -127 end, -1.0, 127.0,
|
||||
'MULK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x // 1 end, 10.0, 10.0,
|
||||
'IDIVK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x % (100 - 10) end, 91, 1,
|
||||
'MODK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
||||
checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1')
|
||||
|
||||
-- K operands in arithmetic operations
|
||||
checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1')
|
||||
-- check(function (x) return 128 + x end, 'ADDK', 'RETURN1')
|
||||
checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1')
|
||||
-- check(function (x) return 20 * x end, 'MULK', 'RETURN1')
|
||||
checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1')
|
||||
checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1')
|
||||
checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1')
|
||||
checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1')
|
||||
checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1')
|
||||
-- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x * -10000 end, 2, -20000,
|
||||
'MULK', 'MMBINK', 'RETURN1')
|
||||
-- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x // 10000 end, 10000, 1,
|
||||
'IDIVK', 'MMBINK', 'RETURN1')
|
||||
checkR(function (x) return x % (100.0 - 10) end, 91, 1.0,
|
||||
'MODK', 'MMBINK', 'RETURN1')
|
||||
|
||||
-- no foldings (and immediate operands)
|
||||
check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1')
|
||||
check(function () return 3/0 end, 'LOADI', 'DIVI', 'RETURN1')
|
||||
check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1')
|
||||
check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1')
|
||||
check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1')
|
||||
check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1')
|
||||
check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1')
|
||||
check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1')
|
||||
check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1')
|
||||
check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1')
|
||||
check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1')
|
||||
check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1')
|
||||
|
||||
-- basic 'for' loops
|
||||
check(function () for i = -10, 10.5 do end end,
|
||||
@@ -335,7 +370,7 @@ end,
|
||||
|
||||
do -- tests for table access in upvalues
|
||||
local t
|
||||
check(function () t.x = t.y end, 'GETTABUP', 'SETTABUP')
|
||||
check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP')
|
||||
check(function (a) t[a()] = t[a()] end,
|
||||
'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL',
|
||||
'GETUPVAL', 'GETTABLE', 'SETTABLE')
|
||||
@@ -355,7 +390,7 @@ check(function (a, b)
|
||||
if b then break else a = a + 1 end
|
||||
end
|
||||
end,
|
||||
'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'JMP', 'RETURN0')
|
||||
'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0')
|
||||
|
||||
checkequal(
|
||||
function (a) while a < 10 do a = a + 1 end end,
|
||||
@@ -379,6 +414,30 @@ function (a)
|
||||
end
|
||||
)
|
||||
|
||||
checkequal(function () return 6 or true or nil end,
|
||||
function () return k6 or kTrue or kNil end)
|
||||
|
||||
checkequal(function () return 6 and true or nil end,
|
||||
function () return k6 and kTrue or kNil end)
|
||||
|
||||
|
||||
do -- string constants
|
||||
local k0 <const> = "00000000000000000000000000000000000000000000000000"
|
||||
local function f1 ()
|
||||
local k <const> = k0
|
||||
return function ()
|
||||
return function () return k end
|
||||
end
|
||||
end
|
||||
|
||||
local f2 = f1()
|
||||
local f3 = f2()
|
||||
assert(f3() == k0)
|
||||
checkK(f3, k0)
|
||||
-- string is not needed by other functions
|
||||
assert(T.listk(f1)[1] == nil)
|
||||
assert(T.listk(f2)[1] == nil)
|
||||
end
|
||||
|
||||
print 'OK'
|
||||
|
||||
|
||||
@@ -211,15 +211,15 @@ assert(a==1 and b==nil)
|
||||
print'+';
|
||||
|
||||
do -- testing constants
|
||||
local <const> prog = [[local <XXX> x = 10]]
|
||||
local prog <const> = [[local x <XXX> = 10]]
|
||||
checkload(prog, "unknown attribute 'XXX'")
|
||||
|
||||
checkload([[local <const> xxx = 20; xxx = 10]],
|
||||
checkload([[local xxx <const> = 20; xxx = 10]],
|
||||
":1: attempt to assign to const variable 'xxx'")
|
||||
|
||||
checkload([[
|
||||
local xx;
|
||||
local <const> xxx = 20;
|
||||
local xxx <const> = 20;
|
||||
local yyy;
|
||||
local function foo ()
|
||||
local abc = xx + yyy + xxx;
|
||||
@@ -228,7 +228,7 @@ do -- testing constants
|
||||
]], ":6: attempt to assign to const variable 'xxx'")
|
||||
|
||||
checkload([[
|
||||
local <toclose> x = nil
|
||||
local x <close> = nil
|
||||
x = io.open()
|
||||
]], ":2: attempt to assign to const variable 'x'")
|
||||
end
|
||||
@@ -287,7 +287,7 @@ a,b = F(nil)==nil; assert(a == true and b == nil)
|
||||
------------------------------------------------------------------
|
||||
|
||||
-- sometimes will be 0, sometimes will not...
|
||||
_ENV.GLOB1 = math.floor(os.time()) % 2
|
||||
_ENV.GLOB1 = math.random(0, 1)
|
||||
|
||||
-- basic expressions with their respective values
|
||||
local basiccases = {
|
||||
@@ -298,16 +298,36 @@ local basiccases = {
|
||||
{"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
|
||||
}
|
||||
|
||||
local prog
|
||||
|
||||
if _ENV.GLOB1 == 0 then
|
||||
basiccases[2][1] = "F" -- constant false
|
||||
|
||||
prog = [[
|
||||
local F <const> = false
|
||||
if %s then IX = true end
|
||||
return %s
|
||||
]]
|
||||
else
|
||||
basiccases[4][1] = "k10" -- constant 10
|
||||
|
||||
prog = [[
|
||||
local k10 <const> = 10
|
||||
if %s then IX = true end
|
||||
return %s
|
||||
]]
|
||||
end
|
||||
|
||||
print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
|
||||
|
||||
|
||||
-- operators with their respective values
|
||||
local <const> binops = {
|
||||
local binops <const> = {
|
||||
{" and ", function (a,b) if not a then return a else return b end end},
|
||||
{" or ", function (a,b) if a then return a else return b end end},
|
||||
}
|
||||
|
||||
local <const> cases = {}
|
||||
local cases <const> = {}
|
||||
|
||||
-- creates all combinations of '(cases[i] op cases[n-i])' plus
|
||||
-- 'not(cases[i] op cases[n-i])' (syntax + value)
|
||||
@@ -337,8 +357,6 @@ cases[1] = basiccases
|
||||
for i = 2, level do cases[i] = createcases(i) end
|
||||
print("+")
|
||||
|
||||
local prog = [[if %s then IX = true end; return %s]]
|
||||
|
||||
local i = 0
|
||||
for n = 1, level do
|
||||
for _, v in pairs(cases[n]) do
|
||||
|
||||
@@ -151,7 +151,7 @@ do
|
||||
end
|
||||
|
||||
co = coroutine.create(function ()
|
||||
local <toclose> x = func2close(function (self, err)
|
||||
local x <close> = func2close(function (self, err)
|
||||
assert(err == nil); X = false
|
||||
end)
|
||||
X = true
|
||||
@@ -163,21 +163,30 @@ do
|
||||
assert(not X and coroutine.status(co) == "dead")
|
||||
|
||||
-- error closing a coroutine
|
||||
warn("@on")
|
||||
local x = 0
|
||||
co = coroutine.create(function()
|
||||
local <toclose> y = func2close(function (self,err)
|
||||
local y <close> = func2close(function (self,err)
|
||||
if (err ~= 111) then os.exit(false) end -- should not happen
|
||||
x = 200
|
||||
error(200)
|
||||
error("200")
|
||||
end)
|
||||
local <toclose> x = func2close(function (self, err)
|
||||
local x <close> = func2close(function (self, err)
|
||||
assert(err == nil); error(111)
|
||||
end)
|
||||
coroutine.yield()
|
||||
end)
|
||||
coroutine.resume(co)
|
||||
assert(x == 0)
|
||||
-- with test library, use 'store' mode to check warnings
|
||||
warn(not T and "@off" or "@store")
|
||||
local st, msg = coroutine.close(co)
|
||||
if not T then
|
||||
warn("@on")
|
||||
else -- test library
|
||||
assert(string.find(_WARN, "200")); _WARN = nil
|
||||
warn("@normal")
|
||||
end
|
||||
assert(st == false and coroutine.status(co) == "dead" and msg == 111)
|
||||
assert(x == 200)
|
||||
|
||||
@@ -356,7 +365,7 @@ do
|
||||
|
||||
local X = false
|
||||
A = coroutine.wrap(function()
|
||||
local <toclose> _ = setmetatable({}, {__close = function () X = true end})
|
||||
local _ <close> = setmetatable({}, {__close = function () X = true end})
|
||||
return pcall(A, 1)
|
||||
end)
|
||||
st, res = A()
|
||||
@@ -426,6 +435,10 @@ else
|
||||
while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y')
|
||||
if A==0 then turn = "A"; assert(T.resume(x)) end
|
||||
if B==0 then turn = "B"; assert(T.resume(y)) end
|
||||
|
||||
-- check that traceback works correctly after yields inside hooks
|
||||
debug.traceback(x)
|
||||
debug.traceback(y)
|
||||
end
|
||||
|
||||
assert(B // A == 7) -- fact(7) // fact(6)
|
||||
@@ -711,6 +724,17 @@ assert(run(function () return a / b end, {"div"}) == 10/12)
|
||||
assert(run(function () return a % b end, {"mod"}) == 10)
|
||||
assert(run(function () return a // b end, {"idiv"}) == 0)
|
||||
|
||||
-- repeat tests with larger constants (to use 'K' opcodes)
|
||||
local a1000 = new(1000)
|
||||
|
||||
assert(run(function () return a1000 + 1000 end, {"add"}) == 2000)
|
||||
assert(run(function () return a1000 - 25000 end, {"sub"}) == -24000)
|
||||
assert(run(function () return 2000 * a end, {"mul"}) == 20000)
|
||||
assert(run(function () return a1000 / 1000 end, {"div"}) == 1)
|
||||
assert(run(function () return a1000 % 600 end, {"mod"}) == 400)
|
||||
assert(run(function () return a1000 // 500 end, {"idiv"}) == 2)
|
||||
|
||||
|
||||
|
||||
assert(run(function () return a % b end, {"mod"}) == 10)
|
||||
|
||||
@@ -723,6 +747,12 @@ assert(run(function () return a >> b end, {"shr"}) == 10 >> 12)
|
||||
assert(run(function () return 10 & b end, {"band"}) == 10 & 12)
|
||||
assert(run(function () return a | 2 end, {"bor"}) == 10 | 2)
|
||||
assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2)
|
||||
assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2)
|
||||
assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10)
|
||||
assert(run(function () return a << 2 end, {"shl"}) == 10 << 2)
|
||||
assert(run(function () return 1 << a end, {"shl"}) == 1 << 10)
|
||||
assert(run(function () return 2 ~ a end, {"bxor"}) == 2 ~ 10)
|
||||
|
||||
|
||||
assert(run(function () return a..b end, {"concat"}) == "1012")
|
||||
|
||||
@@ -733,7 +763,7 @@ assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end,
|
||||
{"concat", "concat", "concat"}) == "ab10chello12x")
|
||||
|
||||
|
||||
do -- a few more tests for comparsion operators
|
||||
do -- a few more tests for comparison operators
|
||||
local mt1 = {
|
||||
__le = function (a,b)
|
||||
coroutine.yield(10)
|
||||
@@ -805,7 +835,7 @@ assert(run(function ()
|
||||
-- tests for coroutine API
|
||||
if T==nil then
|
||||
(Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
||||
return
|
||||
print "OK"; return
|
||||
end
|
||||
|
||||
print('testing coroutine API')
|
||||
|
||||
@@ -1,24 +1,50 @@
|
||||
-- $Id: testes/cstack.lua $
|
||||
-- See Copyright Notice in file all.lua
|
||||
|
||||
local debug = require "debug"
|
||||
|
||||
print"testing C-stack overflow detection"
|
||||
print"If this test crashes, see its file ('cstack.lua')"
|
||||
|
||||
-- Segmentation faults in these tests probably result from a C-stack
|
||||
-- overflow. To avoid these errors, recompile Lua with a smaller
|
||||
-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger
|
||||
-- stack for the program.
|
||||
-- overflow. To avoid these errors, you can use the function
|
||||
-- 'debug.setcstacklimit' to set a smaller limit for the use of
|
||||
-- C stack by Lua. After finding a reliable limit, you might want
|
||||
-- to recompile Lua with this limit as the value for
|
||||
-- the constant 'LUAI_MAXCCALLS', which defines the default limit.
|
||||
-- (The default limit is printed by this test.)
|
||||
-- Alternatively, you can ensure a larger stack for the program.
|
||||
|
||||
-- For Linux, a limit up to 30_000 seems Ok. Windows cannot go much
|
||||
-- higher than 2_000.
|
||||
|
||||
|
||||
-- get and print original limit
|
||||
local origlimit <const> = debug.setcstacklimit(400)
|
||||
print("default stack limit: " .. origlimit)
|
||||
|
||||
|
||||
-- Do the tests using the original limit. Or else you may want to change
|
||||
-- 'currentlimit' to lower values to avoid a seg. fault or to higher
|
||||
-- values to check whether they are reliable.
|
||||
local currentlimit <const> = origlimit
|
||||
debug.setcstacklimit(currentlimit)
|
||||
print("current stack limit: " .. currentlimit)
|
||||
|
||||
|
||||
local function checkerror (msg, f, ...)
|
||||
local s, err = pcall(f, ...)
|
||||
assert(not s and string.find(err, msg))
|
||||
end
|
||||
|
||||
-- auxiliary function to keep 'count' on the screen even if the program
|
||||
-- crashes.
|
||||
local count
|
||||
local back = string.rep("\b", 8)
|
||||
local function progress ()
|
||||
count = count + 1
|
||||
local n = string.format("%-8d", count)
|
||||
io.stderr:write(back, n)
|
||||
io.stderr:write(back, n) -- erase previous value and write new one
|
||||
end
|
||||
|
||||
|
||||
@@ -26,7 +52,7 @@ do print("testing simple recursion:")
|
||||
count = 0
|
||||
local function foo ()
|
||||
progress()
|
||||
foo()
|
||||
foo() -- do recursive calls until a stack error (or crash)
|
||||
end
|
||||
checkerror("stack overflow", foo)
|
||||
print("\tfinal count: ", count)
|
||||
@@ -79,4 +105,56 @@ do print("testing stack-overflow in recursive 'gsub'")
|
||||
print("\tfinal count: ", count)
|
||||
end
|
||||
|
||||
|
||||
do print("testing changes in C-stack limit")
|
||||
|
||||
-- Just an alternative limit, different from the current one
|
||||
-- (smaller to avoid stack overflows)
|
||||
local alterlimit <const> = currentlimit * 8 // 10
|
||||
|
||||
assert(not debug.setcstacklimit(0)) -- limit too small
|
||||
assert(not debug.setcstacklimit(50000)) -- limit too large
|
||||
local co = coroutine.wrap (function ()
|
||||
return debug.setcstacklimit(alterlimit)
|
||||
end)
|
||||
assert(not co()) -- cannot change C stack inside coroutine
|
||||
|
||||
local n
|
||||
local function foo () n = n + 1; foo () end
|
||||
|
||||
local function check ()
|
||||
n = 0
|
||||
pcall(foo)
|
||||
return n
|
||||
end
|
||||
|
||||
-- set limit to 'alterlimit'
|
||||
assert(debug.setcstacklimit(alterlimit) == currentlimit)
|
||||
local limalter <const> = check()
|
||||
-- set a very low limit (given that there are already several active
|
||||
-- calls to arrive here)
|
||||
local lowlimit <const> = 38
|
||||
assert(debug.setcstacklimit(lowlimit) == alterlimit)
|
||||
-- usable limit is much lower, due to active calls
|
||||
local actuallow = check()
|
||||
assert(actuallow < lowlimit - 30)
|
||||
-- now, add 'lowlimit' extra slots, which should all be available
|
||||
assert(debug.setcstacklimit(lowlimit + lowlimit) == lowlimit)
|
||||
local lim2 <const> = check()
|
||||
assert(lim2 == actuallow + lowlimit)
|
||||
|
||||
|
||||
-- 'setcstacklimit' works inside protected calls. (The new stack
|
||||
-- limit is kept when 'pcall' returns.)
|
||||
assert(pcall(function ()
|
||||
assert(debug.setcstacklimit(alterlimit) == lowlimit * 2)
|
||||
assert(check() <= limalter)
|
||||
end))
|
||||
|
||||
assert(check() == limalter)
|
||||
-- restore original limit
|
||||
assert(debug.setcstacklimit(origlimit) == alterlimit)
|
||||
end
|
||||
|
||||
|
||||
print'OK'
|
||||
|
||||
@@ -255,6 +255,10 @@ do -- test hook presence in debug info
|
||||
end
|
||||
|
||||
|
||||
-- hook table has weak keys
|
||||
assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
|
||||
|
||||
|
||||
a = {}; L = nil
|
||||
local glob = 1
|
||||
local oldglob = glob
|
||||
@@ -347,12 +351,12 @@ assert(g(0,0) == 30)
|
||||
|
||||
|
||||
debug.sethook(nil);
|
||||
assert(debug.gethook() == nil)
|
||||
assert(not debug.gethook())
|
||||
|
||||
|
||||
-- minimal tests for setuservalue/getuservalue
|
||||
do
|
||||
assert(debug.setuservalue(io.stdin, 10) == nil)
|
||||
assert(not debug.setuservalue(io.stdin, 10))
|
||||
local a, b = debug.getuservalue(io.stdin, 10)
|
||||
assert(a == nil and not b)
|
||||
end
|
||||
@@ -410,7 +414,7 @@ end, "c")
|
||||
a:f(1,2,3,4,5)
|
||||
assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
|
||||
assert(XX == 12)
|
||||
assert(debug.gethook() == nil)
|
||||
assert(not debug.gethook())
|
||||
|
||||
|
||||
-- testing access to local variables in return hook (bug in 5.2)
|
||||
@@ -645,6 +649,11 @@ t = debug.getinfo(1) -- main
|
||||
assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
|
||||
debug.getupvalue(t.func, 1) == "_ENV")
|
||||
|
||||
t = debug.getinfo(math.sin) -- C function
|
||||
assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
|
||||
|
||||
t = debug.getinfo(string.gmatch("abc", "a")) -- C closure
|
||||
assert(t.isvararg == true and t.nparams == 0 and t.nups > 0)
|
||||
|
||||
|
||||
|
||||
@@ -802,8 +811,7 @@ assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and
|
||||
-a == "unm" and #a == "len" and a&3 == "band")
|
||||
assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
|
||||
-a == "unm" and #a == "len" and a & 3 == "band")
|
||||
assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and
|
||||
a>>1 == "shift")
|
||||
assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr")
|
||||
assert (a==b and a.op == "eq")
|
||||
assert (a>=b and a.op == "order")
|
||||
assert (a>b and a.op == "order")
|
||||
|
||||
@@ -18,7 +18,7 @@ end
|
||||
|
||||
local function doit (s)
|
||||
local f, msg = load(s)
|
||||
if f == nil then return msg end
|
||||
if not f then return msg end
|
||||
local cond, msg = pcall(f)
|
||||
return (not cond) and msg
|
||||
end
|
||||
@@ -312,8 +312,8 @@ end
|
||||
|
||||
local function lineerror (s, l)
|
||||
local err,msg = pcall(load(s))
|
||||
local line = string.match(msg, ":(%d+):")
|
||||
assert(tonumber(line) == l)
|
||||
local line = tonumber(string.match(msg, ":(%d+):"))
|
||||
assert(line == l or (not line and not l))
|
||||
end
|
||||
|
||||
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
|
||||
@@ -359,7 +359,7 @@ local p = [[
|
||||
g()
|
||||
]]
|
||||
X=3;lineerror((p), 3)
|
||||
X=0;lineerror((p), nil)
|
||||
X=0;lineerror((p), false)
|
||||
X=1;lineerror((p), 2)
|
||||
X=2;lineerror((p), 1)
|
||||
|
||||
@@ -510,7 +510,7 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
|
||||
checksyntax("\255a = 1", "", "<\\255>", 1)
|
||||
|
||||
doit('I = load("a=9+"); a=3')
|
||||
assert(a==3 and I == nil)
|
||||
assert(a==3 and not I)
|
||||
print('+')
|
||||
|
||||
lim = 1000
|
||||
@@ -523,9 +523,13 @@ end
|
||||
|
||||
-- testing syntax limits
|
||||
|
||||
local function testrep (init, rep, close, repc)
|
||||
local function testrep (init, rep, close, repc, finalresult)
|
||||
local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
|
||||
assert(load(s)) -- 100 levels is OK
|
||||
local res, msg = load(s)
|
||||
assert(res) -- 100 levels is OK
|
||||
if (finalresult) then
|
||||
assert(res() == finalresult)
|
||||
end
|
||||
s = init .. string.rep(rep, 10000)
|
||||
local res, msg = load(s) -- 10000 levels not ok
|
||||
assert(not res and (string.find(msg, "too many registers") or
|
||||
@@ -534,14 +538,14 @@ end
|
||||
|
||||
testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
|
||||
testrep("local a; a=", "{", "0", "}")
|
||||
testrep("local a; a=", "(", "2", ")")
|
||||
testrep("local a; ", "a(", "2", ")")
|
||||
testrep("return ", "(", "2", ")", 2)
|
||||
testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
|
||||
testrep("", "do ", "", " end")
|
||||
testrep("", "while a do ", "", " end")
|
||||
testrep("local a; ", "if a then else ", "", " end")
|
||||
testrep("", "function foo () ", "", " end")
|
||||
testrep("local a; a=", "a..", "a", "")
|
||||
testrep("local a; a=", "a^", "a", "")
|
||||
testrep("local a = ''; return ", "a..", "'a'", "", "a")
|
||||
testrep("local a = 1; return ", "a^", "a", "", 1)
|
||||
|
||||
checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ assert(~a == a); checkcap{"bnot", a, a}
|
||||
assert(a << 3 == a); checkcap{"shl", a, 3}
|
||||
assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a}
|
||||
|
||||
-- for comparsion operators, all results are true
|
||||
-- for comparison operators, all results are true
|
||||
assert(5.0 > a); checkcap{"lt", a, 5.0}
|
||||
assert(a >= 10); checkcap{"le", 10, a}
|
||||
assert(a <= -10.0); checkcap{"le", a, -10.0}
|
||||
@@ -217,9 +217,16 @@ t.__le = function (a,b,c)
|
||||
return a<=b, "dummy"
|
||||
end
|
||||
|
||||
t.__eq = function (a,b,c)
|
||||
assert(c == nil)
|
||||
if type(a) == 'table' then a = a.x end
|
||||
if type(b) == 'table' then b = b.x end
|
||||
return a == b, "dummy"
|
||||
end
|
||||
|
||||
function Op(x) return setmetatable({x=x}, t) end
|
||||
|
||||
local function test ()
|
||||
local function test (a, b, c)
|
||||
assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
|
||||
assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
|
||||
assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
|
||||
@@ -232,9 +239,13 @@ local function test ()
|
||||
assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
|
||||
assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
|
||||
assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
|
||||
assert(Op(1) == Op(1) and Op(1) ~= Op(2))
|
||||
assert(Op('a') == Op('a') and Op('a') ~= Op('b'))
|
||||
assert(a == a and a ~= b)
|
||||
assert(Op(3) == c)
|
||||
end
|
||||
|
||||
test()
|
||||
test(Op(1), Op(2), Op(3))
|
||||
|
||||
|
||||
-- test `partial order'
|
||||
@@ -314,6 +325,7 @@ else
|
||||
assert(u1 == u3 and u3 == u1 and u1 ~= u2)
|
||||
assert(u2 == u1 and u2 == u3 and u3 == u2)
|
||||
assert(u2 ~= {}) -- different types cannot be equal
|
||||
assert(rawequal(u1, u1) and not rawequal(u1, u3))
|
||||
|
||||
local mirror = {}
|
||||
debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
|
||||
|
||||
117
testes/files.lua
117
testes/files.lua
@@ -125,7 +125,7 @@ do
|
||||
-- closing file by scope
|
||||
local F = nil
|
||||
do
|
||||
local <toclose> f = assert(io.open(file, "w"))
|
||||
local f <close> = assert(io.open(file, "w"))
|
||||
F = f
|
||||
end
|
||||
assert(tostring(F) == "file (closed)")
|
||||
@@ -135,7 +135,7 @@ assert(os.remove(file))
|
||||
|
||||
do
|
||||
-- test writing/reading numbers
|
||||
local <toclose> f = assert(io.open(file, "w"))
|
||||
local f <close> = assert(io.open(file, "w"))
|
||||
f:write(maxint, '\n')
|
||||
f:write(string.format("0X%x\n", maxint))
|
||||
f:write("0xABCp-3", '\n')
|
||||
@@ -144,7 +144,7 @@ do
|
||||
f:write(string.format("0x%X\n", -maxint))
|
||||
f:write("-0xABCp-3", '\n')
|
||||
assert(f:close())
|
||||
local <toclose> f = assert(io.open(file, "r"))
|
||||
local f <close> = assert(io.open(file, "r"))
|
||||
assert(f:read("n") == maxint)
|
||||
assert(f:read("n") == maxint)
|
||||
assert(f:read("n") == 0xABCp-3)
|
||||
@@ -158,7 +158,7 @@ assert(os.remove(file))
|
||||
|
||||
-- testing multiple arguments to io.read
|
||||
do
|
||||
local <toclose> f = assert(io.open(file, "w"))
|
||||
local f <close> = assert(io.open(file, "w"))
|
||||
f:write[[
|
||||
a line
|
||||
another line
|
||||
@@ -170,21 +170,21 @@ three
|
||||
]]
|
||||
local l1, l2, l3, l4, n1, n2, c, dummy
|
||||
assert(f:close())
|
||||
local <toclose> f = assert(io.open(file, "r"))
|
||||
local f <close> = assert(io.open(file, "r"))
|
||||
l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
|
||||
assert(l1 == "a line" and l2 == "another line\n" and
|
||||
n1 == 1234 and n2 == 3.45 and dummy == nil)
|
||||
assert(f:close())
|
||||
local <toclose> f = assert(io.open(file, "r"))
|
||||
local f <close> = assert(io.open(file, "r"))
|
||||
l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
|
||||
assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
|
||||
n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
|
||||
and dummy == nil)
|
||||
assert(f:close())
|
||||
local <toclose> f = assert(io.open(file, "r"))
|
||||
local f <close> = assert(io.open(file, "r"))
|
||||
-- second item failing
|
||||
l1, n1, n2, dummy = f:read("l", "n", "n", "l")
|
||||
assert(l1 == "a line" and n1 == nil)
|
||||
assert(l1 == "a line" and not n1)
|
||||
end
|
||||
assert(os.remove(file))
|
||||
|
||||
@@ -228,7 +228,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n")
|
||||
assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
|
||||
|
||||
do -- attempt to read too long number
|
||||
assert(f:read("n") == nil) -- fails
|
||||
assert(not f:read("n")) -- fails
|
||||
local s = f:read("L") -- read rest of line
|
||||
assert(string.find(s, "^00*\n$")) -- lots of 0's left
|
||||
end
|
||||
@@ -314,13 +314,13 @@ assert(io.read() == "fourth_line")
|
||||
assert(io.read() == "") -- empty line
|
||||
assert(io.read('n') == 3450)
|
||||
assert(io.read(1) == '\n')
|
||||
assert(io.read(0) == nil) -- end of file
|
||||
assert(io.read(1) == nil) -- end of file
|
||||
assert(io.read(30000) == nil) -- end of file
|
||||
assert(not io.read(0)) -- end of file
|
||||
assert(not io.read(1)) -- end of file
|
||||
assert(not io.read(30000)) -- end of file
|
||||
assert(({io.read(1)})[2] == undef)
|
||||
assert(io.read() == nil) -- end of file
|
||||
assert(not io.read()) -- end of file
|
||||
assert(({io.read()})[2] == undef)
|
||||
assert(io.read('n') == nil) -- end of file
|
||||
assert(not io.read('n')) -- end of file
|
||||
assert(({io.read('n')})[2] == undef)
|
||||
assert(io.read('a') == '') -- end of file (OK for 'a')
|
||||
assert(io.read('a') == '') -- end of file (OK for 'a')
|
||||
@@ -356,7 +356,7 @@ assert(io.read(string.len(t)) == t)
|
||||
assert(io.read(1) == ' ')
|
||||
assert(io.read(0))
|
||||
assert(io.read('a') == ';end of file\n')
|
||||
assert(io.read(0) == nil)
|
||||
assert(not io.read(0))
|
||||
assert(io.close(io.input()))
|
||||
|
||||
|
||||
@@ -364,7 +364,7 @@ assert(io.close(io.input()))
|
||||
do
|
||||
local function ismsg (m)
|
||||
-- error message is not a code number
|
||||
return (type(m) == "string" and tonumber(m) == nil)
|
||||
return (type(m) == "string" and not tonumber(m))
|
||||
end
|
||||
|
||||
-- read
|
||||
@@ -393,7 +393,7 @@ assert(io.read"L" == "\n")
|
||||
assert(io.read"L" == "\n")
|
||||
assert(io.read"L" == "line\n")
|
||||
assert(io.read"L" == "other")
|
||||
assert(io.read"L" == nil)
|
||||
assert(not io.read"L")
|
||||
io.input():close()
|
||||
|
||||
local f = assert(io.open(file))
|
||||
@@ -427,10 +427,12 @@ do -- testing closing file in line iteration
|
||||
-- get the to-be-closed variable from a loop
|
||||
local function gettoclose (lv)
|
||||
lv = lv + 1
|
||||
for i = 1, math.maxinteger do
|
||||
local stvar = 0 -- to-be-closed is 4th state variable in the loop
|
||||
for i = 1, 1000 do
|
||||
local n, v = debug.getlocal(lv, i)
|
||||
if n == "(for toclose)" then
|
||||
return v
|
||||
if n == "(for state)" then
|
||||
stvar = stvar + 1
|
||||
if stvar == 4 then return v end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -460,7 +462,7 @@ end
|
||||
-- test for multipe arguments in 'lines'
|
||||
io.output(file); io.write"0123456789\n":close()
|
||||
for a,b in io.lines(file, 1, 1) do
|
||||
if a == "\n" then assert(b == nil)
|
||||
if a == "\n" then assert(not b)
|
||||
else assert(tonumber(a) == tonumber(b) - 1)
|
||||
end
|
||||
end
|
||||
@@ -471,13 +473,13 @@ end
|
||||
|
||||
for a,b,c in io.lines(file, "a", 0, 1) do
|
||||
if a == "" then break end
|
||||
assert(a == "0123456789\n" and b == nil and c == nil)
|
||||
assert(a == "0123456789\n" and not b and not c)
|
||||
end
|
||||
collectgarbage() -- to close file in previous iteration
|
||||
|
||||
io.output(file); io.write"00\n10\n20\n30\n40\n":close()
|
||||
for a, b in io.lines(file, "n", "n") do
|
||||
if a == 40 then assert(b == nil)
|
||||
if a == 40 then assert(not b)
|
||||
else assert(a == b - 10)
|
||||
end
|
||||
end
|
||||
@@ -652,7 +654,7 @@ and the rest of the file
|
||||
io.input(file)
|
||||
local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
|
||||
assert(io.close(io.input()))
|
||||
assert(_ == ' ' and __ == nil)
|
||||
assert(_ == ' ' and not __)
|
||||
assert(type(a) == 'number' and a==123.4 and b==-56e-2)
|
||||
assert(d=='second line' and e=='third line')
|
||||
assert(h==[[
|
||||
@@ -704,7 +706,7 @@ if not _soft then
|
||||
io.input():seek('set', 0)
|
||||
y = io.read() -- huge line
|
||||
assert(x == y..'\n'..io.read())
|
||||
assert(io.read() == nil)
|
||||
assert(not io.read())
|
||||
io.close(io.input())
|
||||
assert(os.remove(file))
|
||||
x = nil; y = nil
|
||||
@@ -773,11 +775,24 @@ assert(os.date(string.rep("%d", 1000), t) ==
|
||||
string.rep(os.date("%d", t), 1000))
|
||||
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
|
||||
|
||||
local t = os.time()
|
||||
D = os.date("*t", t)
|
||||
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||
D.hour==%H and D.min==%M and D.sec==%S and
|
||||
D.wday==%w+1 and D.yday==%j)]], t))()
|
||||
local function checkDateTable (t)
|
||||
_G.D = os.date("*t", t)
|
||||
assert(os.time(D) == t)
|
||||
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||
D.hour==%H and D.min==%M and D.sec==%S and
|
||||
D.wday==%w+1 and D.yday==%j)]], t))()
|
||||
_G.D = nil
|
||||
end
|
||||
|
||||
checkDateTable(os.time())
|
||||
if not _port then
|
||||
-- assume that time_t can represent these values
|
||||
checkDateTable(0)
|
||||
checkDateTable(1)
|
||||
checkDateTable(1000)
|
||||
checkDateTable(0x7fffffff)
|
||||
checkDateTable(0x80000000)
|
||||
end
|
||||
|
||||
checkerr("invalid conversion specifier", os.date, "%")
|
||||
checkerr("invalid conversion specifier", os.date, "%9")
|
||||
@@ -791,11 +806,24 @@ checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5})
|
||||
|
||||
checkerr("missing", os.time, {hour = 12}) -- missing date
|
||||
|
||||
|
||||
if string.packsize("i") == 4 then -- 4-byte ints
|
||||
checkerr("field 'year' is out-of-bound", os.time,
|
||||
{year = -(1 << 31) + 1899, month = 1, day = 1})
|
||||
end
|
||||
|
||||
if not _port then
|
||||
-- test Posix-specific modifiers
|
||||
assert(type(os.date("%Ex")) == 'string')
|
||||
assert(type(os.date("%Oy")) == 'string')
|
||||
|
||||
-- test large dates (assume at least 4-byte ints and time_t)
|
||||
local t0 = os.time{year = 1970, month = 1, day = 0}
|
||||
local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1}
|
||||
assert(t1 - t0 == (1 << 31) - 1)
|
||||
t0 = os.time{year = 1970, month = 1, day = 1}
|
||||
t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)}
|
||||
assert(t1 - t0 == -(1 << 31))
|
||||
|
||||
-- test out-of-range dates (at least for Unix)
|
||||
if maxint >= 2^62 then -- cannot do these tests in Small Lua
|
||||
@@ -810,25 +838,37 @@ if not _port then
|
||||
-- time_t has 8 bytes; an int year cannot represent a huge time
|
||||
print(" 8-byte time_t")
|
||||
checkerr("cannot be represented", os.date, "%Y", 2^60)
|
||||
-- it should have no problems with year 4000
|
||||
assert(tonumber(os.time{year=4000, month=1, day=1}))
|
||||
|
||||
-- this is the maximum year
|
||||
assert(tonumber(os.time
|
||||
{year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59}))
|
||||
|
||||
-- this is too much
|
||||
checkerr("represented", os.time,
|
||||
{year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60})
|
||||
end
|
||||
|
||||
-- internal 'int' fields cannot hold these values
|
||||
checkerr("field 'day' is out-of-bound", os.time,
|
||||
{year = 0, month = 1, day = 2^32})
|
||||
|
||||
checkerr("field 'month' is out-of-bound", os.time,
|
||||
{year = 0, month = -((1 << 31) + 1), day = 1})
|
||||
|
||||
checkerr("field 'year' is out-of-bound", os.time,
|
||||
{year = (1 << 31) + 1900, month = 1, day = 1})
|
||||
|
||||
else -- 8-byte ints
|
||||
-- assume time_t has 8 bytes too
|
||||
print(" 8-byte time_t")
|
||||
assert(tonumber(os.date("%Y", 2^60)))
|
||||
|
||||
-- but still cannot represent a huge year
|
||||
checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
D = os.date("!*t", t)
|
||||
load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||
D.hour==%H and D.min==%M and D.sec==%S and
|
||||
D.wday==%w+1 and D.yday==%j)]], t))()
|
||||
|
||||
do
|
||||
local D = os.date("*t")
|
||||
local t = os.time(D)
|
||||
@@ -842,6 +882,7 @@ do
|
||||
assert(t == t1) -- if isdst is absent uses correct default
|
||||
end
|
||||
|
||||
local D = os.date("*t")
|
||||
t = os.time(D)
|
||||
D.year = D.year-1;
|
||||
local t1 = os.time(D)
|
||||
|
||||
@@ -99,35 +99,28 @@ local function GC() GC1(); GC2() end
|
||||
do
|
||||
print("creating many objects")
|
||||
|
||||
local contCreate = 0
|
||||
|
||||
local limit = 5000
|
||||
|
||||
while contCreate <= limit do
|
||||
for i = 1, limit do
|
||||
local a = {}; a = nil
|
||||
contCreate = contCreate+1
|
||||
end
|
||||
|
||||
local a = "a"
|
||||
|
||||
contCreate = 0
|
||||
while contCreate <= limit do
|
||||
a = contCreate .. "b";
|
||||
for i = 1, limit do
|
||||
a = i .. "b";
|
||||
a = string.gsub(a, '(%d%d*)', "%1 %1")
|
||||
a = "a"
|
||||
contCreate = contCreate+1
|
||||
end
|
||||
|
||||
|
||||
contCreate = 0
|
||||
|
||||
a = {}
|
||||
|
||||
function a:test ()
|
||||
while contCreate <= limit do
|
||||
load(string.format("function temp(a) return 'a%d' end", contCreate), "")()
|
||||
assert(temp() == string.format('a%d', contCreate))
|
||||
contCreate = contCreate+1
|
||||
for i = 1, limit do
|
||||
load(string.format("function temp(a) return 'a%d' end", i), "")()
|
||||
assert(temp() == string.format('a%d', i))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -166,9 +159,8 @@ print('long strings')
|
||||
x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||
assert(string.len(x)==80)
|
||||
s = ''
|
||||
n = 0
|
||||
k = math.min(300, (math.maxinteger // 80) // 2)
|
||||
while n < k do s = s..x; n=n+1; j=tostring(n) end
|
||||
for n = 1, k do s = s..x; j=tostring(n) end
|
||||
assert(string.len(s) == k*80)
|
||||
s = string.sub(s, 1, 10000)
|
||||
s, i = string.gsub(s, '(%d%d%d%d)', '')
|
||||
@@ -377,9 +369,10 @@ if T then
|
||||
s[n] = i
|
||||
end
|
||||
|
||||
warn("@on"); warn("@store")
|
||||
collectgarbage()
|
||||
assert(string.find(_WARN, "error in __gc metamethod"))
|
||||
assert(string.match(_WARN, "@(.-)@") == "expected")
|
||||
assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = nil
|
||||
for i = 8, 10 do assert(s[i]) end
|
||||
|
||||
for i = 1, 5 do
|
||||
@@ -391,6 +384,7 @@ if T then
|
||||
for i = 1, 10 do assert(s[i]) end
|
||||
|
||||
getmetatable(u).__gc = nil
|
||||
warn("@normal")
|
||||
|
||||
end
|
||||
print '+'
|
||||
@@ -483,9 +477,12 @@ end
|
||||
|
||||
-- errors during collection
|
||||
if T then
|
||||
warn("@store")
|
||||
u = setmetatable({}, {__gc = function () error "@expected error" end})
|
||||
u = nil
|
||||
collectgarbage()
|
||||
assert(string.find(_WARN, "@expected error")); _WARN = nil
|
||||
warn("@normal")
|
||||
end
|
||||
|
||||
|
||||
@@ -643,7 +640,7 @@ do
|
||||
assert(getmetatable(o) == tt)
|
||||
-- create new objects during GC
|
||||
local a = 'xuxu'..(10+3)..'joao', {}
|
||||
___Glob = o -- ressurect object!
|
||||
___Glob = o -- ressurrect object!
|
||||
setmetatable({}, tt) -- creates a new one with same metatable
|
||||
print(">>> closing state " .. "<<<\n")
|
||||
end
|
||||
@@ -653,7 +650,7 @@ end
|
||||
|
||||
-- create several objects to raise errors when collected while closing state
|
||||
if T then
|
||||
local error, assert, find = error, assert, string.find
|
||||
local error, assert, find, warn = error, assert, string.find, warn
|
||||
local n = 0
|
||||
local lastmsg
|
||||
local mt = {__gc = function (o)
|
||||
@@ -667,6 +664,7 @@ if T then
|
||||
else
|
||||
assert(lastmsg == _WARN) -- subsequent error messages are equal
|
||||
end
|
||||
warn("@store"); _WARN = nil
|
||||
error"@expected warning"
|
||||
end}
|
||||
for i = 10, 1, -1 do
|
||||
|
||||
@@ -258,7 +258,7 @@ do
|
||||
::L2:: goto L3
|
||||
|
||||
::L1:: do
|
||||
local <toclose> a = setmetatable({}, {__close = function () X = true end})
|
||||
local a <close> = setmetatable({}, {__close = function () X = true end})
|
||||
assert(X == nil)
|
||||
if a then goto L2 end -- jumping back out of scope of 'a'
|
||||
end
|
||||
|
||||
@@ -11,17 +11,17 @@ CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared
|
||||
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
|
||||
touch all
|
||||
|
||||
lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
||||
lib1.so: lib1.c $(LUA_DIR)/luaconf.h
|
||||
$(CC) $(CFLAGS) -o lib1.so lib1.c
|
||||
|
||||
lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
||||
lib11.so: lib11.c $(LUA_DIR)/luaconf.h
|
||||
$(CC) $(CFLAGS) -o lib11.so lib11.c
|
||||
|
||||
lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
||||
lib2.so: lib2.c $(LUA_DIR)/luaconf.h
|
||||
$(CC) $(CFLAGS) -o lib2.so lib2.c
|
||||
|
||||
lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
||||
lib21.so: lib21.c $(LUA_DIR)/luaconf.h
|
||||
$(CC) $(CFLAGS) -o lib21.so lib21.c
|
||||
|
||||
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h
|
||||
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h
|
||||
$(CC) $(CFLAGS) -o lib2-v2.so lib22.c
|
||||
|
||||
@@ -281,7 +281,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then
|
||||
|
||||
assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
|
||||
|
||||
assert(tonumber"inf" == nil and tonumber"NAN" == nil)
|
||||
assert(not tonumber"inf" and not tonumber"NAN")
|
||||
|
||||
assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ assert(c.a == nil)
|
||||
f()
|
||||
assert(c.a == 3)
|
||||
|
||||
-- old test for limits for special instructions (now just a generic test)
|
||||
-- old test for limits for special instructions
|
||||
do
|
||||
local i = 2
|
||||
local p = 4 -- p == 2^i
|
||||
@@ -114,7 +114,7 @@ if rawget(_G, "T") then
|
||||
local t = T.querytab(a)
|
||||
|
||||
for k,_ in pairs(a) do a[k] = undef end
|
||||
collectgarbage() -- restore GC and collect dead fiels in `a'
|
||||
collectgarbage() -- restore GC and collect dead fields in 'a'
|
||||
for i=0,t-1 do
|
||||
local k = querytab(a, i)
|
||||
assert(k == nil or type(k) == 'number' or k == 'alo')
|
||||
@@ -173,24 +173,57 @@ end
|
||||
assert(x==20)
|
||||
|
||||
|
||||
do -- constants
|
||||
local a<const>, b, c<const> = 10, 20, 30
|
||||
b = a + c + b -- 'b' is not constant
|
||||
assert(a == 10 and b == 60 and c == 30)
|
||||
local function checkro (name, code)
|
||||
local st, msg = load(code)
|
||||
local gab = string.format("attempt to assign to const variable '%s'", name)
|
||||
assert(not st and string.find(msg, gab))
|
||||
end
|
||||
checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12")
|
||||
checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11")
|
||||
checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11")
|
||||
|
||||
checkro("z", [[
|
||||
local a, z <const>, b = 10;
|
||||
function foo() a = 20; z = 32; end
|
||||
]])
|
||||
|
||||
checkro("var1", [[
|
||||
local a, var1 <const> = 10;
|
||||
function foo() a = 20; z = function () var1 = 12; end end
|
||||
]])
|
||||
end
|
||||
|
||||
|
||||
print"testing to-be-closed variables"
|
||||
|
||||
local function stack(n) n = ((n == 0) or stack(n - 1)) end
|
||||
|
||||
local function func2close (f)
|
||||
return setmetatable({}, {__close = f})
|
||||
local function func2close (f, x, y)
|
||||
local obj = setmetatable({}, {__close = f})
|
||||
if x then
|
||||
return x, obj, y
|
||||
else
|
||||
return obj
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
local a = {}
|
||||
do
|
||||
local <toclose> x = setmetatable({"x"}, {__close = function (self)
|
||||
local b <close> = false -- not to be closed
|
||||
local x <close> = setmetatable({"x"}, {__close = function (self)
|
||||
a[#a + 1] = self[1] end})
|
||||
local <toclose> y = func2close(function (self, err)
|
||||
assert(err == nil); a[#a + 1] = "y"
|
||||
end)
|
||||
local w, y <close>, z = func2close(function (self, err)
|
||||
assert(err == nil); a[#a + 1] = "y"
|
||||
end, 10, 20)
|
||||
local c <close> = nil -- not to be closed
|
||||
a[#a + 1] = "in"
|
||||
assert(w == 10 and z == 20)
|
||||
end
|
||||
a[#a + 1] = "out"
|
||||
assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out")
|
||||
@@ -199,11 +232,12 @@ end
|
||||
do
|
||||
local X = false
|
||||
|
||||
local closescope = func2close(function () stack(10); X = true end)
|
||||
local x, closescope = func2close(function () stack(10); X = true end, 100)
|
||||
assert(x == 100); x = 101; -- 'x' is not read-only
|
||||
|
||||
-- closing functions do not corrupt returning values
|
||||
local function foo (x)
|
||||
local <toclose> _ = closescope
|
||||
local _ <close> = closescope
|
||||
return x, X, 23
|
||||
end
|
||||
|
||||
@@ -212,7 +246,7 @@ do
|
||||
|
||||
X = false
|
||||
foo = function (x)
|
||||
local <toclose> _ = closescope
|
||||
local _<close> = closescope
|
||||
local y = 15
|
||||
return y
|
||||
end
|
||||
@@ -221,7 +255,7 @@ do
|
||||
|
||||
X = false
|
||||
foo = function ()
|
||||
local <toclose> x = closescope
|
||||
local x <close> = closescope
|
||||
return x
|
||||
end
|
||||
|
||||
@@ -230,17 +264,54 @@ do
|
||||
end
|
||||
|
||||
|
||||
-- testing to-be-closed x compile-time constants
|
||||
-- (there were some bugs here in Lua 5.4-rc3, due to a confusion
|
||||
-- between compile levels and stack levels of variables)
|
||||
do
|
||||
local flag = false
|
||||
local x = setmetatable({},
|
||||
{__close = function() assert(flag == false); flag = true end})
|
||||
local y <const> = nil
|
||||
local z <const> = nil
|
||||
do
|
||||
local a <close> = x
|
||||
end
|
||||
assert(flag) -- 'x' must be closed here
|
||||
end
|
||||
|
||||
do
|
||||
-- similar problem, but with implicit close in for loops
|
||||
local flag = false
|
||||
local x = setmetatable({},
|
||||
{__close = function () assert(flag == false); flag = true end})
|
||||
-- return an empty iterator, nil, nil, and 'x' to be closed
|
||||
local function a ()
|
||||
return (function () return nil end), nil, nil, x
|
||||
end
|
||||
local v <const> = 1
|
||||
local w <const> = 1
|
||||
local x <const> = 1
|
||||
local y <const> = 1
|
||||
local z <const> = 1
|
||||
for k in a() do
|
||||
a = k
|
||||
end -- ending the loop must close 'x'
|
||||
assert(flag) -- 'x' must be closed here
|
||||
end
|
||||
|
||||
|
||||
|
||||
do
|
||||
-- calls cannot be tail in the scope of to-be-closed variables
|
||||
local X, Y
|
||||
local function foo ()
|
||||
local <toclose> _ = func2close(function () Y = 10 end)
|
||||
local _ <close> = func2close(function () Y = 10 end)
|
||||
assert(X == true and Y == nil) -- 'X' not closed yet
|
||||
return 1,2,3
|
||||
end
|
||||
|
||||
local function bar ()
|
||||
local <toclose> _ = func2close(function () X = false end)
|
||||
local _ <close> = func2close(function () X = false end)
|
||||
X = true
|
||||
do
|
||||
return foo() -- not a tail call!
|
||||
@@ -252,69 +323,187 @@ do
|
||||
end
|
||||
|
||||
|
||||
do -- errors in __close
|
||||
local log = {}
|
||||
local function foo (err)
|
||||
local <toclose> x =
|
||||
func2close(function (self, msg) log[#log + 1] = msg; error(1) end)
|
||||
local <toclose> x1 =
|
||||
func2close(function (self, msg) log[#log + 1] = msg; end)
|
||||
local <toclose> gc = func2close(function () collectgarbage() end)
|
||||
local <toclose> y =
|
||||
func2close(function (self, msg) log[#log + 1] = msg; error(2) end)
|
||||
local <toclose> z =
|
||||
func2close(function (self, msg) log[#log + 1] = msg or 10; error(3) end)
|
||||
if err then error(4) end
|
||||
-- auxiliary functions for testing warnings in '__close'
|
||||
local function prepwarn ()
|
||||
if not T then -- no test library?
|
||||
warn("@off") -- do not show (lots of) warnings
|
||||
else
|
||||
warn("@store") -- to test the warnings
|
||||
end
|
||||
local stat, msg = pcall(foo, false)
|
||||
assert(msg == 3)
|
||||
assert(log[1] == 10 and log[2] == 3 and log[3] == 3 and log[4] == 3
|
||||
and #log == 4)
|
||||
|
||||
log = {}
|
||||
local stat, msg = pcall(foo, true)
|
||||
assert(msg == 4)
|
||||
assert(log[1] == 4 and log[2] == 4 and log[3] == 4 and log[4] == 4
|
||||
and #log == 4)
|
||||
|
||||
-- error in toclose in vararg function
|
||||
function foo (...)
|
||||
local <toclose> x123 = 10
|
||||
end
|
||||
|
||||
local st, msg = pcall(foo)
|
||||
assert(string.find(msg, "'x123'"))
|
||||
|
||||
end
|
||||
|
||||
|
||||
do
|
||||
local function endwarn ()
|
||||
if not T then
|
||||
warn("@on") -- back to normal
|
||||
else
|
||||
assert(_WARN == nil)
|
||||
warn("@normal")
|
||||
end
|
||||
end
|
||||
|
||||
-- errors due to non-closable values
|
||||
|
||||
local function checkwarn (msg)
|
||||
if T then
|
||||
assert(string.find(_WARN, msg))
|
||||
_WARN = nil -- reset variable to check next warning
|
||||
end
|
||||
end
|
||||
|
||||
warn("@on")
|
||||
|
||||
do print("testing errors in __close")
|
||||
|
||||
prepwarn()
|
||||
|
||||
-- original error is in __close
|
||||
local function foo ()
|
||||
local <toclose> x = 34
|
||||
|
||||
local x <close> =
|
||||
func2close(function (self, msg)
|
||||
assert(string.find(msg, "@z"))
|
||||
error("@x")
|
||||
end)
|
||||
|
||||
local x1 <close> =
|
||||
func2close(function (self, msg)
|
||||
checkwarn("@y")
|
||||
assert(string.find(msg, "@z"))
|
||||
end)
|
||||
|
||||
local gc <close> = func2close(function () collectgarbage() end)
|
||||
|
||||
local y <close> =
|
||||
func2close(function (self, msg)
|
||||
assert(string.find(msg, "@z")) -- first error in 'z'
|
||||
checkwarn("@z") -- second error in 'z' generated a warning
|
||||
error("@y")
|
||||
end)
|
||||
|
||||
local first = true
|
||||
local z <close> =
|
||||
-- 'z' close is called twice
|
||||
func2close(function (self, msg)
|
||||
if first then
|
||||
assert(msg == nil)
|
||||
first = false
|
||||
else
|
||||
assert(string.find(msg, "@z")) -- own error
|
||||
end
|
||||
error("@z")
|
||||
end)
|
||||
|
||||
return 200
|
||||
end
|
||||
|
||||
local stat, msg = pcall(foo, false)
|
||||
assert(string.find(msg, "@z"))
|
||||
checkwarn("@x")
|
||||
|
||||
|
||||
-- original error not in __close
|
||||
local function foo ()
|
||||
|
||||
local x <close> =
|
||||
func2close(function (self, msg)
|
||||
assert(msg == 4)
|
||||
end)
|
||||
|
||||
local x1 <close> =
|
||||
func2close(function (self, msg)
|
||||
checkwarn("@y")
|
||||
assert(msg == 4)
|
||||
error("@x1")
|
||||
end)
|
||||
|
||||
local gc <close> = func2close(function () collectgarbage() end)
|
||||
|
||||
local y <close> =
|
||||
func2close(function (self, msg)
|
||||
assert(msg == 4) -- error in body
|
||||
checkwarn("@z")
|
||||
error("@y")
|
||||
end)
|
||||
|
||||
local first = true
|
||||
local z <close> =
|
||||
func2close(function (self, msg)
|
||||
-- 'z' close is called once
|
||||
assert(first and msg == 4)
|
||||
first = false
|
||||
error("@z")
|
||||
end)
|
||||
|
||||
error(4) -- original error
|
||||
end
|
||||
|
||||
local stat, msg = pcall(foo, true)
|
||||
assert(msg == 4)
|
||||
checkwarn("@x1") -- last error
|
||||
|
||||
-- error leaving a block
|
||||
local function foo (...)
|
||||
do
|
||||
local x1 <close> =
|
||||
func2close(function ()
|
||||
checkwarn("@X")
|
||||
error("@Y")
|
||||
end)
|
||||
|
||||
local x123 <close> =
|
||||
func2close(function ()
|
||||
error("@X")
|
||||
end)
|
||||
end
|
||||
os.exit(false) -- should not run
|
||||
end
|
||||
|
||||
local st, msg = xpcall(foo, debug.traceback)
|
||||
assert(string.match(msg, "^[^ ]* @X"))
|
||||
assert(string.find(msg, "in metamethod 'close'"))
|
||||
checkwarn("@Y")
|
||||
|
||||
-- error in toclose in vararg function
|
||||
local function foo (...)
|
||||
local x123 <close> = func2close(function () error("@x123") end)
|
||||
end
|
||||
|
||||
local st, msg = xpcall(foo, debug.traceback)
|
||||
assert(string.match(msg, "^[^ ]* @x123"))
|
||||
assert(string.find(msg, "in metamethod 'close'"))
|
||||
checkwarn("@x123") -- from second call to close 'x123'
|
||||
|
||||
endwarn()
|
||||
end
|
||||
|
||||
|
||||
do -- errors due to non-closable values
|
||||
local function foo ()
|
||||
local x <close> = {}
|
||||
os.exit(false) -- should not run
|
||||
end
|
||||
local stat, msg = pcall(foo)
|
||||
assert(not stat and string.find(msg, "variable 'x'"))
|
||||
assert(not stat and
|
||||
string.find(msg, "variable 'x' got a non%-closable value"))
|
||||
|
||||
|
||||
-- with other errors, non-closable values are ignored
|
||||
local function foo ()
|
||||
local <toclose> x = 34
|
||||
local <toclose> y = func2close(function () error(32) end)
|
||||
local xyz <close> = setmetatable({}, {__close = print})
|
||||
getmetatable(xyz).__close = nil -- remove metamethod
|
||||
end
|
||||
local stat, msg = pcall(foo)
|
||||
assert(not stat and msg == 32)
|
||||
|
||||
assert(not stat and
|
||||
string.find(msg, "attempt to close non%-closable variable 'xyz'"))
|
||||
end
|
||||
|
||||
|
||||
if rawget(_G, "T") then
|
||||
|
||||
warn("@off")
|
||||
|
||||
-- memory error inside closing function
|
||||
local function foo ()
|
||||
local <toclose> y = func2close(function () T.alloccount() end)
|
||||
local <toclose> x = setmetatable({}, {__close = function ()
|
||||
local y <close> = func2close(function () T.alloccount() end)
|
||||
local x <close> = setmetatable({}, {__close = function ()
|
||||
T.alloccount(0); local x = {} -- force a memory error
|
||||
end})
|
||||
error(1000) -- common error inside the function's body
|
||||
@@ -340,7 +529,7 @@ if rawget(_G, "T") then
|
||||
end
|
||||
|
||||
local function test ()
|
||||
local <toclose> x = enter(0) -- set a memory limit
|
||||
local x <close> = enter(0) -- set a memory limit
|
||||
-- creation of previous upvalue will raise a memory error
|
||||
assert(false) -- should not run
|
||||
end
|
||||
@@ -355,14 +544,14 @@ if rawget(_G, "T") then
|
||||
|
||||
-- repeat test with extra closing upvalues
|
||||
local function test ()
|
||||
local <toclose> xxx = func2close(function (self, msg)
|
||||
local xxx <close> = func2close(function (self, msg)
|
||||
assert(msg == "not enough memory");
|
||||
error(1000) -- raise another error
|
||||
end)
|
||||
local <toclose> xx = func2close(function (self, msg)
|
||||
local xx <close> = func2close(function (self, msg)
|
||||
assert(msg == "not enough memory");
|
||||
end)
|
||||
local <toclose> x = enter(0) -- set a memory limit
|
||||
local x <close> = enter(0) -- set a memory limit
|
||||
-- creation of previous upvalue will raise a memory error
|
||||
os.exit(false) -- should not run
|
||||
end
|
||||
@@ -387,7 +576,7 @@ if rawget(_G, "T") then
|
||||
|
||||
local s = string.rep("a", lim)
|
||||
|
||||
-- concat this table needs two buffer resizes (one for each 's')
|
||||
-- concat this table needs two buffer resizes (one for each 's')
|
||||
local a = {s, s}
|
||||
|
||||
collectgarbage()
|
||||
@@ -422,6 +611,8 @@ if rawget(_G, "T") then
|
||||
|
||||
print'+'
|
||||
end
|
||||
|
||||
warn("@on")
|
||||
end
|
||||
|
||||
|
||||
@@ -432,9 +623,9 @@ do
|
||||
local x = false
|
||||
local y = false
|
||||
local co = coroutine.wrap(function ()
|
||||
local <toclose> xv = func2close(function () x = true end)
|
||||
local xv <close> = func2close(function () x = true end)
|
||||
do
|
||||
local <toclose> yv = func2close(function () y = true end)
|
||||
local yv <close> = func2close(function () y = true end)
|
||||
coroutine.yield(100) -- yield doesn't close variable
|
||||
end
|
||||
coroutine.yield(200) -- yield doesn't close variable
|
||||
@@ -451,36 +642,45 @@ end
|
||||
|
||||
|
||||
do
|
||||
prepwarn()
|
||||
|
||||
-- error in a wrapped coroutine raising errors when closing a variable
|
||||
local x = 0
|
||||
local co = coroutine.wrap(function ()
|
||||
local <toclose> xx = func2close(function () x = x + 1; error("YYY") end)
|
||||
local <toclose> xv = func2close(function () x = x + 1; error("XXX") end)
|
||||
local xx <close> = func2close(function () x = x + 1; error("@YYY") end)
|
||||
local xv <close> = func2close(function () x = x + 1; error("@XXX") end)
|
||||
coroutine.yield(100)
|
||||
error(200)
|
||||
end)
|
||||
assert(co() == 100); assert(x == 0)
|
||||
local st, msg = pcall(co); assert(x == 2)
|
||||
assert(not st and msg == 200) -- should get first error raised
|
||||
checkwarn("@YYY")
|
||||
|
||||
x = 0
|
||||
local x = 0
|
||||
local y = 0
|
||||
co = coroutine.wrap(function ()
|
||||
local <toclose> xx = func2close(function () x = x + 1; error("YYY") end)
|
||||
local <toclose> xv = func2close(function () x = x + 1; error("XXX") end)
|
||||
local xx <close> = func2close(function () y = y + 1; error("YYY") end)
|
||||
local xv <close> = func2close(function () x = x + 1; error("XXX") end)
|
||||
coroutine.yield(100)
|
||||
return 200
|
||||
end)
|
||||
assert(co() == 100); assert(x == 0)
|
||||
local st, msg = pcall(co); assert(x == 2)
|
||||
local st, msg = pcall(co)
|
||||
assert(x == 2 and y == 1) -- first close is called twice
|
||||
-- should get first error raised
|
||||
assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX"))
|
||||
checkwarn("YYY")
|
||||
|
||||
endwarn()
|
||||
end
|
||||
|
||||
|
||||
-- a suspended coroutine should not close its variables when collected
|
||||
local co
|
||||
co = coroutine.wrap(function()
|
||||
local <toclose> x = function () os.exit(false) end -- should not run
|
||||
-- should not run
|
||||
local x <close> = func2close(function () os.exit(false) end)
|
||||
co = nil
|
||||
coroutine.yield()
|
||||
end)
|
||||
@@ -517,27 +717,15 @@ do
|
||||
end
|
||||
assert(s == 35 and numopen == 0)
|
||||
|
||||
-- repeat test with '__open' metamethod instead of a function
|
||||
local function open (x)
|
||||
numopen = numopen + 1
|
||||
local state = setmetatable({x},
|
||||
{__close = function () numopen = numopen - 1 end})
|
||||
return
|
||||
function (t) -- iteraction function
|
||||
t[1] = t[1] - 1
|
||||
if t[1] > 0 then return t[1] end
|
||||
end,
|
||||
state,
|
||||
nil,
|
||||
state -- to-be-closed
|
||||
end
|
||||
|
||||
local s = 0
|
||||
for i in open(10) do
|
||||
if (i < 5) then break end
|
||||
s = s + i
|
||||
for j in open(10) do
|
||||
if i + j < 5 then goto endloop end
|
||||
s = s + i
|
||||
end
|
||||
end
|
||||
assert(s == 35 and numopen == 0)
|
||||
::endloop::
|
||||
assert(s == 375 and numopen == 0)
|
||||
end
|
||||
|
||||
print('OK')
|
||||
|
||||
@@ -43,6 +43,8 @@ local function getoutput ()
|
||||
end
|
||||
|
||||
local function checkprogout (s)
|
||||
-- expected result must end with new line
|
||||
assert(string.sub(s, -1) == "\n")
|
||||
local t = getoutput()
|
||||
for line in string.gmatch(s, ".-\n") do
|
||||
assert(string.find(t, line, 1, true))
|
||||
@@ -219,6 +221,42 @@ assert(string.find(getoutput(), "error calling 'print'"))
|
||||
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
|
||||
checkout("lua_debug> 1000lua_debug> ")
|
||||
|
||||
|
||||
print("testing warnings")
|
||||
|
||||
-- no warnings by default
|
||||
RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua 2> %s', out)
|
||||
checkout("1")
|
||||
|
||||
prepfile[[
|
||||
warn("@allow") -- unknown control, ignored
|
||||
warn("@off", "XXX", "@off") -- these are not control messages
|
||||
warn("@off") -- this one is
|
||||
warn("@on", "YYY", "@on") -- not control, but warn is off
|
||||
warn("@off") -- keep it off
|
||||
warn("@on") -- restart warnings
|
||||
warn("", "@on") -- again, no control, real warning
|
||||
warn("@on") -- keep it "started"
|
||||
warn("Z", "Z", "Z") -- common warning
|
||||
]]
|
||||
RUN('lua -W %s 2> %s', prog, out)
|
||||
checkout[[
|
||||
Lua warning: @offXXX@off
|
||||
Lua warning: @on
|
||||
Lua warning: ZZZ
|
||||
]]
|
||||
|
||||
prepfile[[
|
||||
warn("@allow")
|
||||
-- create two objects to be finalized when closing state
|
||||
-- the errors in the finalizers must generate warnings
|
||||
u1 = setmetatable({}, {__gc = function () error("XYZ") end})
|
||||
u2 = setmetatable({}, {__gc = function () error("ZYX") end})
|
||||
]]
|
||||
RUN('lua -W %s 2> %s', prog, out)
|
||||
checkprogout("ZYX)\nXYZ)\n")
|
||||
|
||||
|
||||
-- test many arguments
|
||||
prepfile[[print(({...})[30])]]
|
||||
RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
|
||||
@@ -292,7 +330,7 @@ debug = require"debug"
|
||||
print(debug.getinfo(1).currentline)
|
||||
]]
|
||||
RUN('lua %s > %s', prog, out)
|
||||
checkprogout('3')
|
||||
checkprogout('3\n')
|
||||
|
||||
-- close Lua with an open file
|
||||
prepfile(string.format([[io.output(%q); io.write('alo')]], out))
|
||||
@@ -320,15 +358,16 @@ NoRun("", "lua %s", prog) -- no message
|
||||
|
||||
-- to-be-closed variables in main chunk
|
||||
prepfile[[
|
||||
local <toclose> x = function (err)
|
||||
assert(err == 120)
|
||||
print("Ok")
|
||||
end
|
||||
local <toclose> e1 = function () error(120) end
|
||||
local x <close> = setmetatable({},
|
||||
{__close = function (self, err)
|
||||
assert(err == nil)
|
||||
print("Ok")
|
||||
end})
|
||||
local e1 <close> = setmetatable({}, {__close = function () print(120) end})
|
||||
os.exit(true, true)
|
||||
]]
|
||||
RUN('lua %s > %s', prog, out)
|
||||
checkprogout("Ok")
|
||||
checkprogout("120\nOk\n")
|
||||
|
||||
|
||||
-- remove temporary files
|
||||
@@ -352,8 +391,15 @@ if T then -- test library?
|
||||
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
||||
|
||||
-- testing 'warn'
|
||||
warn("@store")
|
||||
warn("@123", "456", "789")
|
||||
assert(_WARN == "@123456789")
|
||||
assert(_WARN == "@123456789"); _WARN = nil
|
||||
|
||||
warn("zip", "", " ", "zap")
|
||||
assert(_WARN == "zip zap"); _WARN = nil
|
||||
warn("ZIP", "", " ", "ZAP")
|
||||
assert(_WARN == "ZIP ZAP"); _WARN = nil
|
||||
warn("@normal")
|
||||
end
|
||||
|
||||
do
|
||||
|
||||
141
testes/math.lua
141
testes/math.lua
@@ -3,10 +3,10 @@
|
||||
|
||||
print("testing numbers and math lib")
|
||||
|
||||
local <const> minint = math.mininteger
|
||||
local <const> maxint = math.maxinteger
|
||||
local minint <const> = math.mininteger
|
||||
local maxint <const> = math.maxinteger
|
||||
|
||||
local <const> intbits = math.floor(math.log(maxint, 2) + 0.5) + 1
|
||||
local intbits <const> = math.floor(math.log(maxint, 2) + 0.5) + 1
|
||||
assert((1 << intbits) == 0)
|
||||
|
||||
assert(minint == 1 << (intbits - 1))
|
||||
@@ -39,7 +39,7 @@ do
|
||||
end
|
||||
|
||||
assert(math.type(0) == "integer" and math.type(0.0) == "float"
|
||||
and math.type("10") == nil)
|
||||
and not math.type("10"))
|
||||
|
||||
|
||||
local function checkerror (msg, f, ...)
|
||||
@@ -270,7 +270,7 @@ else
|
||||
end
|
||||
|
||||
do
|
||||
local NaN = 0/0
|
||||
local NaN <const> = 0/0
|
||||
assert(not (NaN < 0))
|
||||
assert(not (NaN > minint))
|
||||
assert(not (NaN <= -9))
|
||||
@@ -381,17 +381,17 @@ assert(tonumber(1/0) == 1/0)
|
||||
|
||||
-- 'tonumber' with strings
|
||||
assert(tonumber("0") == 0)
|
||||
assert(tonumber("") == nil)
|
||||
assert(tonumber(" ") == nil)
|
||||
assert(tonumber("-") == nil)
|
||||
assert(tonumber(" -0x ") == nil)
|
||||
assert(tonumber{} == nil)
|
||||
assert(not tonumber(""))
|
||||
assert(not tonumber(" "))
|
||||
assert(not tonumber("-"))
|
||||
assert(not tonumber(" -0x "))
|
||||
assert(not tonumber{})
|
||||
assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
|
||||
tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
|
||||
tonumber'+1.' == 1)
|
||||
assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
|
||||
tonumber'1e' == nil and tonumber'1.0e+' == nil and
|
||||
tonumber'.' == nil)
|
||||
assert(not tonumber'+ 0.01' and not tonumber'+.e1' and
|
||||
not tonumber'1e' and not tonumber'1.0e+' and
|
||||
not tonumber'.')
|
||||
assert(tonumber('-012') == -010-2)
|
||||
assert(tonumber('-1.2e2') == - - -120)
|
||||
|
||||
@@ -445,45 +445,45 @@ local function f (...)
|
||||
end
|
||||
end
|
||||
|
||||
assert(f(tonumber('fFfa', 15)) == nil)
|
||||
assert(f(tonumber('099', 8)) == nil)
|
||||
assert(f(tonumber('1\0', 2)) == nil)
|
||||
assert(f(tonumber('', 8)) == nil)
|
||||
assert(f(tonumber(' ', 9)) == nil)
|
||||
assert(f(tonumber(' ', 9)) == nil)
|
||||
assert(f(tonumber('0xf', 10)) == nil)
|
||||
assert(not f(tonumber('fFfa', 15)))
|
||||
assert(not f(tonumber('099', 8)))
|
||||
assert(not f(tonumber('1\0', 2)))
|
||||
assert(not f(tonumber('', 8)))
|
||||
assert(not f(tonumber(' ', 9)))
|
||||
assert(not f(tonumber(' ', 9)))
|
||||
assert(not f(tonumber('0xf', 10)))
|
||||
|
||||
assert(f(tonumber('inf')) == nil)
|
||||
assert(f(tonumber(' INF ')) == nil)
|
||||
assert(f(tonumber('Nan')) == nil)
|
||||
assert(f(tonumber('nan')) == nil)
|
||||
assert(not f(tonumber('inf')))
|
||||
assert(not f(tonumber(' INF ')))
|
||||
assert(not f(tonumber('Nan')))
|
||||
assert(not f(tonumber('nan')))
|
||||
|
||||
assert(f(tonumber(' ')) == nil)
|
||||
assert(f(tonumber('')) == nil)
|
||||
assert(f(tonumber('1 a')) == nil)
|
||||
assert(f(tonumber('1 a', 2)) == nil)
|
||||
assert(f(tonumber('1\0')) == nil)
|
||||
assert(f(tonumber('1 \0')) == nil)
|
||||
assert(f(tonumber('1\0 ')) == nil)
|
||||
assert(f(tonumber('e1')) == nil)
|
||||
assert(f(tonumber('e 1')) == nil)
|
||||
assert(f(tonumber(' 3.4.5 ')) == nil)
|
||||
assert(not f(tonumber(' ')))
|
||||
assert(not f(tonumber('')))
|
||||
assert(not f(tonumber('1 a')))
|
||||
assert(not f(tonumber('1 a', 2)))
|
||||
assert(not f(tonumber('1\0')))
|
||||
assert(not f(tonumber('1 \0')))
|
||||
assert(not f(tonumber('1\0 ')))
|
||||
assert(not f(tonumber('e1')))
|
||||
assert(not f(tonumber('e 1')))
|
||||
assert(not f(tonumber(' 3.4.5 ')))
|
||||
|
||||
|
||||
-- testing 'tonumber' for invalid hexadecimal formats
|
||||
|
||||
assert(tonumber('0x') == nil)
|
||||
assert(tonumber('x') == nil)
|
||||
assert(tonumber('x3') == nil)
|
||||
assert(tonumber('0x3.3.3') == nil) -- two decimal points
|
||||
assert(tonumber('00x2') == nil)
|
||||
assert(tonumber('0x 2') == nil)
|
||||
assert(tonumber('0 x2') == nil)
|
||||
assert(tonumber('23x') == nil)
|
||||
assert(tonumber('- 0xaa') == nil)
|
||||
assert(tonumber('-0xaaP ') == nil) -- no exponent
|
||||
assert(tonumber('0x0.51p') == nil)
|
||||
assert(tonumber('0x5p+-2') == nil)
|
||||
assert(not tonumber('0x'))
|
||||
assert(not tonumber('x'))
|
||||
assert(not tonumber('x3'))
|
||||
assert(not tonumber('0x3.3.3')) -- two decimal points
|
||||
assert(not tonumber('00x2'))
|
||||
assert(not tonumber('0x 2'))
|
||||
assert(not tonumber('0 x2'))
|
||||
assert(not tonumber('23x'))
|
||||
assert(not tonumber('- 0xaa'))
|
||||
assert(not tonumber('-0xaaP ')) -- no exponent
|
||||
assert(not tonumber('0x0.51p'))
|
||||
assert(not tonumber('0x5p+-2'))
|
||||
|
||||
|
||||
-- testing hexadecimal numerals
|
||||
@@ -705,19 +705,19 @@ do -- testing floor & ceil
|
||||
assert(eqT(math.tointeger(maxint), maxint))
|
||||
assert(eqT(math.tointeger(maxint .. ""), maxint))
|
||||
assert(eqT(math.tointeger(minint + 0.0), minint))
|
||||
assert(math.tointeger(0.0 - minint) == nil)
|
||||
assert(math.tointeger(math.pi) == nil)
|
||||
assert(math.tointeger(-math.pi) == nil)
|
||||
assert(not math.tointeger(0.0 - minint))
|
||||
assert(not math.tointeger(math.pi))
|
||||
assert(not math.tointeger(-math.pi))
|
||||
assert(math.floor(math.huge) == math.huge)
|
||||
assert(math.ceil(math.huge) == math.huge)
|
||||
assert(math.tointeger(math.huge) == nil)
|
||||
assert(not math.tointeger(math.huge))
|
||||
assert(math.floor(-math.huge) == -math.huge)
|
||||
assert(math.ceil(-math.huge) == -math.huge)
|
||||
assert(math.tointeger(-math.huge) == nil)
|
||||
assert(not math.tointeger(-math.huge))
|
||||
assert(math.tointeger("34.0") == 34)
|
||||
assert(math.tointeger("34.3") == nil)
|
||||
assert(math.tointeger({}) == nil)
|
||||
assert(math.tointeger(0/0) == nil) -- NaN
|
||||
assert(not math.tointeger("34.3"))
|
||||
assert(not math.tointeger({}))
|
||||
assert(not math.tointeger(0/0)) -- NaN
|
||||
end
|
||||
|
||||
|
||||
@@ -758,7 +758,7 @@ do -- testing max/min
|
||||
assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
|
||||
assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
|
||||
end
|
||||
-- testing implicit convertions
|
||||
-- testing implicit conversions
|
||||
|
||||
local a,b = '10', '20'
|
||||
assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
|
||||
@@ -767,7 +767,8 @@ assert(a == '10' and b == '20')
|
||||
|
||||
do
|
||||
print("testing -0 and NaN")
|
||||
local mz, z = -0.0, 0.0
|
||||
local mz <const> = -0.0
|
||||
local z <const> = 0.0
|
||||
assert(mz == z)
|
||||
assert(1/mz < 0 and 0 < 1/z)
|
||||
local a = {[mz] = 1}
|
||||
@@ -775,17 +776,18 @@ do
|
||||
a[z] = 2
|
||||
assert(a[z] == 2 and a[mz] == 2)
|
||||
local inf = math.huge * 2 + 1
|
||||
mz, z = -1/inf, 1/inf
|
||||
local mz <const> = -1/inf
|
||||
local z <const> = 1/inf
|
||||
assert(mz == z)
|
||||
assert(1/mz < 0 and 0 < 1/z)
|
||||
local NaN = inf - inf
|
||||
local NaN <const> = inf - inf
|
||||
assert(NaN ~= NaN)
|
||||
assert(not (NaN < NaN))
|
||||
assert(not (NaN <= NaN))
|
||||
assert(not (NaN > NaN))
|
||||
assert(not (NaN >= NaN))
|
||||
assert(not (0 < NaN) and not (NaN < 0))
|
||||
local NaN1 = 0/0
|
||||
local NaN1 <const> = 0/0
|
||||
assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
|
||||
local a = {}
|
||||
assert(not pcall(rawset, a, NaN, 1))
|
||||
@@ -813,9 +815,9 @@ end
|
||||
-- low-level!! For the current implementation of random in Lua,
|
||||
-- the first call after seed 1007 should return 0x7a7040a5a323c9d6
|
||||
do
|
||||
-- all computations assume at most 32-bit integers
|
||||
local h = 0x7a7040a5 -- higher half
|
||||
local l = 0xa323c9d6 -- lower half
|
||||
-- all computations should work with 32-bit integers
|
||||
local h <const> = 0x7a7040a5 -- higher half
|
||||
local l <const> = 0xa323c9d6 -- lower half
|
||||
|
||||
math.randomseed(1007)
|
||||
-- get the low 'intbits' of the 64-bit expected result
|
||||
@@ -838,7 +840,16 @@ do
|
||||
assert(rand * 2^floatbits == res)
|
||||
end
|
||||
|
||||
math.randomseed()
|
||||
do
|
||||
-- testing return of 'randomseed'
|
||||
local x, y = math.randomseed()
|
||||
local res = math.random(0)
|
||||
x, y = math.randomseed(x, y) -- should repeat the state
|
||||
assert(math.random(0) == res)
|
||||
math.randomseed(x, y) -- again should repeat the state
|
||||
assert(math.random(0) == res)
|
||||
-- keep the random seed for following tests
|
||||
end
|
||||
|
||||
do -- test random for floats
|
||||
local randbits = math.min(floatbits, 64) -- at most 64 random bits
|
||||
@@ -949,7 +960,10 @@ do
|
||||
aux(-10,0)
|
||||
aux(1, 6)
|
||||
aux(1, 2)
|
||||
aux(1, 13)
|
||||
aux(1, 31)
|
||||
aux(1, 32)
|
||||
aux(1, 33)
|
||||
aux(-10, 10)
|
||||
aux(-10,-10) -- unit set
|
||||
aux(minint, minint) -- unit set
|
||||
@@ -987,6 +1001,7 @@ do
|
||||
end
|
||||
aux(0, maxint)
|
||||
aux(1, maxint)
|
||||
aux(3, maxint // 3)
|
||||
aux(minint, -1)
|
||||
aux(minint // 2, maxint // 2)
|
||||
aux(minint, maxint)
|
||||
|
||||
@@ -49,33 +49,13 @@ if not T then
|
||||
else --[
|
||||
-- testing table sizes
|
||||
|
||||
local function log2 (x) return math.log(x, 2) end
|
||||
|
||||
local function mp2 (n) -- minimum power of 2 >= n
|
||||
local mp = 2^math.ceil(log2(n))
|
||||
local mp = 2^math.ceil(math.log(n, 2))
|
||||
assert(n == 0 or (mp/2 < n and n <= mp))
|
||||
return mp
|
||||
end
|
||||
|
||||
local function fb (n)
|
||||
local r, nn = T.int2fb(n)
|
||||
assert(r < 256)
|
||||
return nn
|
||||
end
|
||||
|
||||
-- test fb function
|
||||
for a = 1, 10000 do -- all numbers up to 10^4
|
||||
local n = fb(a)
|
||||
assert(a <= n and n <= a*1.125)
|
||||
end
|
||||
local a = 1024 -- plus a few up to 2 ^30
|
||||
local lim = 2^30
|
||||
while a < lim do
|
||||
local n = fb(a)
|
||||
assert(a <= n and n <= a*1.125)
|
||||
a = math.ceil(a*1.3)
|
||||
end
|
||||
|
||||
|
||||
local function check (t, na, nh)
|
||||
local a, h = T.querytab(t)
|
||||
@@ -95,30 +75,44 @@ end
|
||||
|
||||
|
||||
-- testing constructor sizes
|
||||
local lim = 40
|
||||
local s = 'return {'
|
||||
for i=1,lim do
|
||||
s = s..i..','
|
||||
local s = s
|
||||
for k=0,lim do
|
||||
local t = load(s..'}', '')()
|
||||
assert(#t == i)
|
||||
check(t, fb(i), mp2(k))
|
||||
s = string.format('%sa%d=%d,', s, k, k)
|
||||
local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17,
|
||||
30, 31, 32, 33, 34, 254, 255, 256, 500, 1000}
|
||||
|
||||
for _, sa in ipairs(sizes) do -- 'sa' is size of the array part
|
||||
local arr = {"return {"}
|
||||
for i = 1, sa do arr[1 + i] = "1," end -- build array part
|
||||
for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part
|
||||
for j = 1, sh do -- build hash part
|
||||
arr[1 + sa + j] = string.format('k%x=%d,', j, j)
|
||||
end
|
||||
arr[1 + sa + sh + 1] = "}"
|
||||
local prog = table.concat(arr)
|
||||
local f = assert(load(prog))
|
||||
f() -- call once to ensure stack space
|
||||
-- make sure table is not resized after being created
|
||||
if sa == 0 or sh == 0 then
|
||||
T.alloccount(2); -- header + array or hash part
|
||||
else
|
||||
T.alloccount(3); -- header + array part + hash part
|
||||
end
|
||||
local t = f()
|
||||
T.alloccount();
|
||||
assert(#t == sa)
|
||||
check(t, sa, mp2(sh))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- tests with unknown number of elements
|
||||
local a = {}
|
||||
for i=1,lim do a[i] = i end -- build auxiliary table
|
||||
for k=0,lim do
|
||||
local a = {table.unpack(a,1,k)}
|
||||
assert(#a == k)
|
||||
check(a, k, 0)
|
||||
a = {1,2,3,table.unpack(a,1,k)}
|
||||
check(a, k+3, 0)
|
||||
assert(#a == k + 3)
|
||||
for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table
|
||||
for k in ipairs(sizes) do
|
||||
local t = {table.unpack(a,1,k)}
|
||||
assert(#t == k)
|
||||
check(t, k, 0)
|
||||
t = {1,2,3,table.unpack(a,1,k)}
|
||||
check(t, k+3, 0)
|
||||
assert(#t == k + 3)
|
||||
end
|
||||
|
||||
|
||||
@@ -677,7 +671,8 @@ collectgarbage()
|
||||
|
||||
local function f (n, p)
|
||||
local t = {}; for i=1,p do t[i] = i*10 end
|
||||
return function (_,n)
|
||||
return function (_, n, ...)
|
||||
assert(select("#", ...) == 0) -- no extra arguments
|
||||
if n > 0 then
|
||||
n = n-1
|
||||
return n, table.unpack(t)
|
||||
|
||||
54
testes/packtests
Executable file
54
testes/packtests
Executable file
@@ -0,0 +1,54 @@
|
||||
NAME=$1"-tests"
|
||||
|
||||
ln -s . $NAME
|
||||
ln -s .. ltests
|
||||
|
||||
tar --create --gzip --no-recursion --file=$NAME.tar.gz \
|
||||
$NAME/all.lua \
|
||||
$NAME/api.lua \
|
||||
$NAME/attrib.lua \
|
||||
$NAME/big.lua \
|
||||
$NAME/bitwise.lua \
|
||||
$NAME/bwcoercion.lua \
|
||||
$NAME/calls.lua \
|
||||
$NAME/closure.lua \
|
||||
$NAME/code.lua \
|
||||
$NAME/constructs.lua \
|
||||
$NAME/coroutine.lua \
|
||||
$NAME/cstack.lua \
|
||||
$NAME/db.lua \
|
||||
$NAME/errors.lua \
|
||||
$NAME/events.lua \
|
||||
$NAME/files.lua \
|
||||
$NAME/gc.lua \
|
||||
$NAME/gengc.lua \
|
||||
$NAME/goto.lua \
|
||||
$NAME/heavy.lua \
|
||||
$NAME/literals.lua \
|
||||
$NAME/locals.lua \
|
||||
$NAME/main.lua \
|
||||
$NAME/math.lua \
|
||||
$NAME/nextvar.lua \
|
||||
$NAME/pm.lua \
|
||||
$NAME/sort.lua \
|
||||
$NAME/strings.lua \
|
||||
$NAME/tpack.lua \
|
||||
$NAME/utf8.lua \
|
||||
$NAME/vararg.lua \
|
||||
$NAME/verybig.lua \
|
||||
$NAME/libs/makefile \
|
||||
$NAME/libs/P1 \
|
||||
$NAME/libs/lib1.c \
|
||||
$NAME/libs/lib11.c \
|
||||
$NAME/libs/lib2.c \
|
||||
$NAME/libs/lib21.c \
|
||||
$NAME/libs/lib22.c \
|
||||
$NAME/ltests/ltests.h \
|
||||
$NAME/ltests/ltests.c
|
||||
|
||||
\rm -I $NAME
|
||||
\rm -I ltests
|
||||
|
||||
echo ${NAME}.tar.gz" created"
|
||||
|
||||
|
||||
@@ -28,10 +28,10 @@ a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end
|
||||
assert(a == 9 and b == 11);
|
||||
a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position
|
||||
assert(a == 11 and b == 11)
|
||||
assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending
|
||||
assert(string.find('', '\0') == nil)
|
||||
assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending
|
||||
assert(not string.find('', '\0'))
|
||||
assert(string.find('alo123alo', '12') == 4)
|
||||
assert(string.find('alo123alo', '^12') == nil)
|
||||
assert(not string.find('alo123alo', '^12'))
|
||||
|
||||
assert(string.match("aaab", ".*b") == "aaab")
|
||||
assert(string.match("aaa", ".*a") == "aaa")
|
||||
@@ -57,17 +57,17 @@ assert(f('aaa', 'ab*a') == 'aa')
|
||||
assert(f('aba', 'ab*a') == 'aba')
|
||||
assert(f('aaab', 'a+') == 'aaa')
|
||||
assert(f('aaa', '^.+$') == 'aaa')
|
||||
assert(f('aaa', 'b+') == nil)
|
||||
assert(f('aaa', 'ab+a') == nil)
|
||||
assert(not f('aaa', 'b+'))
|
||||
assert(not f('aaa', 'ab+a'))
|
||||
assert(f('aba', 'ab+a') == 'aba')
|
||||
assert(f('a$a', '.$') == 'a')
|
||||
assert(f('a$a', '.%$') == 'a$')
|
||||
assert(f('a$a', '.$.') == 'a$a')
|
||||
assert(f('a$a', '$$') == nil)
|
||||
assert(f('a$b', 'a$') == nil)
|
||||
assert(not f('a$a', '$$'))
|
||||
assert(not f('a$b', 'a$'))
|
||||
assert(f('a$a', '$') == '')
|
||||
assert(f('', 'b*') == '')
|
||||
assert(f('aaa', 'bb*') == nil)
|
||||
assert(not f('aaa', 'bb*'))
|
||||
assert(f('aaab', 'a-') == '')
|
||||
assert(f('aaa', '^.-$') == 'aaa')
|
||||
assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
|
||||
@@ -101,7 +101,7 @@ end
|
||||
assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o")
|
||||
assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3')
|
||||
assert(f1('=======', '^(=*)=%1$') == '=======')
|
||||
assert(string.match('==========', '^([=]*)=%1$') == nil)
|
||||
assert(not string.match('==========', '^([=]*)=%1$'))
|
||||
|
||||
local function range (i, j)
|
||||
if i <= j then
|
||||
@@ -135,7 +135,7 @@ print('+');
|
||||
assert(string.match("alo xyzK", "(%w+)K") == "xyz")
|
||||
assert(string.match("254 K", "(%d*)K") == "")
|
||||
assert(string.match("alo ", "(%w*)$") == "")
|
||||
assert(string.match("alo ", "(%w+)$") == nil)
|
||||
assert(not string.match("alo ", "(%w+)$"))
|
||||
assert(string.find("(álo)", "%(á") == 1)
|
||||
local a, b, c, d, e = string.match("âlo alo", "^(((.).).* (%w*))$")
|
||||
assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil)
|
||||
@@ -209,7 +209,7 @@ assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
|
||||
|
||||
|
||||
function isbalanced (s)
|
||||
return string.find(string.gsub(s, "%b()", ""), "[()]") == nil
|
||||
return not string.find(string.gsub(s, "%b()", ""), "[()]")
|
||||
end
|
||||
|
||||
assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
print('testing strings and string library')
|
||||
|
||||
local <const> maxi = math.maxinteger
|
||||
local <const> mini = math.mininteger
|
||||
local maxi <const> = math.maxinteger
|
||||
local mini <const> = math.mininteger
|
||||
|
||||
|
||||
local function checkerror (msg, f, ...)
|
||||
@@ -56,13 +56,13 @@ a,b = string.find("123456789", "345")
|
||||
assert(string.sub("123456789", a, b) == "345")
|
||||
assert(string.find("1234567890123456789", "345", 3) == 3)
|
||||
assert(string.find("1234567890123456789", "345", 4) == 13)
|
||||
assert(string.find("1234567890123456789", "346", 4) == nil)
|
||||
assert(not string.find("1234567890123456789", "346", 4))
|
||||
assert(string.find("1234567890123456789", ".45", -9) == 13)
|
||||
assert(string.find("abcdefg", "\0", 5, 1) == nil)
|
||||
assert(not string.find("abcdefg", "\0", 5, 1))
|
||||
assert(string.find("", "") == 1)
|
||||
assert(string.find("", "", 1) == 1)
|
||||
assert(not string.find("", "", 2))
|
||||
assert(string.find('', 'aaa', 1) == nil)
|
||||
assert(not string.find('', 'aaa', 1))
|
||||
assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
|
||||
|
||||
assert(string.len("") == 0)
|
||||
@@ -158,17 +158,38 @@ do -- tests for '%p' format
|
||||
-- not much to test, as C does not specify what '%p' does.
|
||||
-- ("The value of the pointer is converted to a sequence of printing
|
||||
-- characters, in an implementation-defined manner.")
|
||||
local null = string.format("%p", nil)
|
||||
assert(string.format("%p", {}) ~= null)
|
||||
local null = "(null)" -- nulls are formatted by Lua
|
||||
assert(string.format("%p", 4) == null)
|
||||
assert(string.format("%p", true) == null)
|
||||
assert(string.format("%p", nil) == null)
|
||||
assert(string.format("%p", {}) ~= null)
|
||||
assert(string.format("%p", print) ~= null)
|
||||
assert(string.format("%p", coroutine.running()) ~= null)
|
||||
assert(string.format("%p", {}) ~= string.format("%p", {}))
|
||||
assert(string.format("%p", string.rep("a", 10)) ==
|
||||
string.format("%p", string.rep("a", 10))) -- short strings
|
||||
assert(string.format("%p", string.rep("a", 300)) ~=
|
||||
string.format("%p", string.rep("a", 300))) -- long strings
|
||||
assert(string.format("%p", io.stdin) ~= null)
|
||||
assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
|
||||
assert(string.format("%p", print) == string.format("%p", print))
|
||||
assert(string.format("%p", print) ~= string.format("%p", assert))
|
||||
|
||||
assert(#string.format("%90p", {}) == 90)
|
||||
assert(#string.format("%-60p", {}) == 60)
|
||||
assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null)
|
||||
assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null))
|
||||
|
||||
do
|
||||
local t1 = {}; local t2 = {}
|
||||
assert(string.format("%p", t1) ~= string.format("%p", t2))
|
||||
end
|
||||
|
||||
do -- short strings are internalized
|
||||
local s1 = string.rep("a", 10)
|
||||
local s2 = string.rep("aa", 5)
|
||||
assert(string.format("%p", s1) == string.format("%p", s2))
|
||||
end
|
||||
|
||||
do -- long strings aren't internalized
|
||||
local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
|
||||
assert(string.format("%p", s1) ~= string.format("%p", s2))
|
||||
end
|
||||
end
|
||||
|
||||
x = '"ílo"\n\\'
|
||||
@@ -250,6 +271,12 @@ do -- longest number that can be formatted
|
||||
local s = string.format('%.99f', -(10^i))
|
||||
assert(string.len(s) >= i + 101)
|
||||
assert(tonumber(s) == -(10^i))
|
||||
|
||||
-- limit for floats
|
||||
assert(10^38 < math.huge)
|
||||
local s = string.format('%.99f', -(10^38))
|
||||
assert(string.len(s) >= 38 + 101)
|
||||
assert(tonumber(s) == -(10^38))
|
||||
end
|
||||
|
||||
|
||||
@@ -295,8 +322,8 @@ do print("testing 'format %a %A'")
|
||||
matchhexa(n)
|
||||
end
|
||||
|
||||
assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$"))
|
||||
assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$"))
|
||||
assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$"))
|
||||
assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$"))
|
||||
|
||||
if not _port then -- test inf, -inf, NaN, and -0.0
|
||||
assert(string.find(string.format("%a", 1/0), "^inf"))
|
||||
|
||||
@@ -30,8 +30,8 @@ local function checksyntax (s, t)
|
||||
assert(assert(load(ts))() == s)
|
||||
end
|
||||
|
||||
assert(utf8.offset("alo", 5) == nil)
|
||||
assert(utf8.offset("alo", -4) == nil)
|
||||
assert(not utf8.offset("alo", 5))
|
||||
assert(not utf8.offset("alo", -4))
|
||||
|
||||
-- 'check' makes several tests over the validity of string 's'.
|
||||
-- 't' is the list of codepoints of 's'.
|
||||
@@ -115,17 +115,17 @@ do
|
||||
end
|
||||
|
||||
-- error in initial position for offset
|
||||
checkerror("position out of range", utf8.offset, "abc", 1, 5)
|
||||
checkerror("position out of range", utf8.offset, "abc", 1, -4)
|
||||
checkerror("position out of range", utf8.offset, "", 1, 2)
|
||||
checkerror("position out of range", utf8.offset, "", 1, -1)
|
||||
checkerror("position out of bounds", utf8.offset, "abc", 1, 5)
|
||||
checkerror("position out of bounds", utf8.offset, "abc", 1, -4)
|
||||
checkerror("position out of bounds", utf8.offset, "", 1, 2)
|
||||
checkerror("position out of bounds", utf8.offset, "", 1, -1)
|
||||
checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
|
||||
checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
|
||||
checkerror("continuation byte", utf8.offset, "\x80", 1)
|
||||
|
||||
-- error in indices for len
|
||||
checkerror("out of string", utf8.len, "abc", 0, 2)
|
||||
checkerror("out of string", utf8.len, "abc", 1, 4)
|
||||
checkerror("out of bounds", utf8.len, "abc", 0, 2)
|
||||
checkerror("out of bounds", utf8.len, "abc", 1, 4)
|
||||
|
||||
|
||||
local s = "hello World"
|
||||
@@ -140,11 +140,11 @@ do
|
||||
local t = {utf8.codepoint(s,1,#s - 1)}
|
||||
assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
|
||||
checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
|
||||
checkerror("out of range", utf8.codepoint, s, #s + 1)
|
||||
checkerror("out of bounds", utf8.codepoint, s, #s + 1)
|
||||
t = {utf8.codepoint(s, 4, 3)}
|
||||
assert(#t == 0)
|
||||
checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1)
|
||||
checkerror("out of range", utf8.codepoint, s, 1, #s + 1)
|
||||
checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1)
|
||||
checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1)
|
||||
-- surrogates
|
||||
assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
|
||||
assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
|
||||
|
||||
Reference in New Issue
Block a user