mirror of
https://github.com/lua/lua.git
synced 2026-07-31 02:19:07 +00:00
Compare commits
63 Commits
| 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 |
78
lapi.c
78
lapi.c
@@ -230,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);
|
||||
@@ -262,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);
|
||||
}
|
||||
|
||||
@@ -350,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;
|
||||
}
|
||||
|
||||
@@ -399,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;
|
||||
}
|
||||
}
|
||||
@@ -448,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))
|
||||
@@ -576,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);
|
||||
}
|
||||
@@ -850,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);
|
||||
@@ -877,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;
|
||||
@@ -1314,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] */
|
||||
@@ -1322,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;
|
||||
@@ -1385,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];
|
||||
|
||||
2
lapi.h
2
lapi.h
@@ -19,7 +19,7 @@
|
||||
|
||||
/*
|
||||
** If a call returns too many multiple returns, the callee may not have
|
||||
** stack space to accomodate all results. In this case, this macro
|
||||
** stack space to accommodate all results. In this case, this macro
|
||||
** increases its stack space ('L->ci->top').
|
||||
*/
|
||||
#define adjustresults(L,nres) \
|
||||
|
||||
12
lauxlib.c
12
lauxlib.c
@@ -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 {
|
||||
@@ -284,7 +284,7 @@ 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 */
|
||||
@@ -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,8 +995,10 @@ 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 */
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
|
||||
/* global table */
|
||||
#define LUA_GNAME "_G"
|
||||
#define LUA_GNAME "_G"
|
||||
|
||||
|
||||
typedef struct luaL_Buffer luaL_Buffer;
|
||||
@@ -185,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);
|
||||
|
||||
88
lcode.c
88
lcode.c
@@ -84,8 +84,11 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
|
||||
if (hasjumps(e))
|
||||
return 0; /* not a constant */
|
||||
switch (e->k) {
|
||||
case VFALSE: case VTRUE:
|
||||
setbvalue(v, e->k == VTRUE);
|
||||
case VFALSE:
|
||||
setbfvalue(v);
|
||||
return 1;
|
||||
case VTRUE:
|
||||
setbtvalue(v);
|
||||
return 1;
|
||||
case VNIL:
|
||||
setnilvalue(v);
|
||||
@@ -110,7 +113,7 @@ int luaK_exp2const (FuncState *fs, 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
|
||||
@@ -604,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 */
|
||||
}
|
||||
|
||||
@@ -653,7 +666,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
||||
|
||||
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
|
||||
lua_Integer fi;
|
||||
if (luaV_flttointeger(f, &fi, 0) && fitsBx(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));
|
||||
@@ -665,19 +678,22 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) {
|
||||
*/
|
||||
static void const2exp (TValue *v, expdesc *e) {
|
||||
switch (ttypetag(v)) {
|
||||
case LUA_TNUMINT:
|
||||
case LUA_VNUMINT:
|
||||
e->k = VKINT; e->u.ival = ivalue(v);
|
||||
break;
|
||||
case LUA_TNUMFLT:
|
||||
case LUA_VNUMFLT:
|
||||
e->k = VKFLT; e->u.nval = fltvalue(v);
|
||||
break;
|
||||
case LUA_TBOOLEAN:
|
||||
e->k = bvalue(v) ? VTRUE : VFALSE;
|
||||
case LUA_VFALSE:
|
||||
e->k = VFALSE;
|
||||
break;
|
||||
case LUA_TNIL:
|
||||
case LUA_VTRUE:
|
||||
e->k = VTRUE;
|
||||
break;
|
||||
case LUA_VNIL:
|
||||
e->k = VNIL;
|
||||
break;
|
||||
case LUA_TSHRSTR: case LUA_TLNGSTR:
|
||||
case LUA_VSHRSTR: case LUA_VLNGSTR:
|
||||
e->k = VKSTR; e->u.strval = tsvalue(v);
|
||||
break;
|
||||
default: lua_assert(0);
|
||||
@@ -687,19 +703,18 @@ static void const2exp (TValue *v, expdesc *e) {
|
||||
|
||||
/*
|
||||
** 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -801,8 +816,12 @@ 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: {
|
||||
@@ -852,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -888,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);
|
||||
}
|
||||
@@ -963,8 +982,8 @@ 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;
|
||||
@@ -1220,7 +1239,7 @@ 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 && luaV_flttointeger(e->u.nval, &i, 0))
|
||||
else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
|
||||
*isfloat = 1;
|
||||
else
|
||||
return 0; /* not a number */
|
||||
@@ -1509,7 +1528,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
||||
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 = im; /* immediate operand */
|
||||
@@ -1710,17 +1729,12 @@ void luaK_fixline (FuncState *fs, int line) {
|
||||
}
|
||||
|
||||
|
||||
void luaK_settablesize (FuncState *fs, int pc, int ra, int rc, int rb) {
|
||||
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
|
||||
Instruction *inst = &fs->f->code[pc];
|
||||
int extra = 0;
|
||||
int k = 0;
|
||||
if (rb != 0)
|
||||
rb = luaO_ceillog2(rb) + 1; /* hash size */
|
||||
if (rc > MAXARG_C) { /* does it need the extra argument? */
|
||||
extra = rc / (MAXARG_C + 1);
|
||||
rc %= (MAXARG_C + 1);
|
||||
k = 1;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
2
lcode.h
2
lcode.h
@@ -95,7 +95,7 @@ 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 rb, int rc);
|
||||
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);
|
||||
|
||||
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 /* } */
|
||||
|
||||
6
ldblib.c
6
ldblib.c
@@ -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");
|
||||
@@ -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 */
|
||||
}
|
||||
}
|
||||
|
||||
30
ldebug.c
30
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': {
|
||||
@@ -775,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);
|
||||
}
|
||||
|
||||
|
||||
16
ldo.c
16
ldo.c
@@ -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))
|
||||
|
||||
|
||||
/*
|
||||
@@ -459,20 +459,20 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
|
||||
lua_CFunction f;
|
||||
retry:
|
||||
switch (ttypetag(s2v(func))) {
|
||||
case LUA_TCCL: /* C closure */
|
||||
case LUA_VCCL: /* C closure */
|
||||
f = clCvalue(s2v(func))->f;
|
||||
goto Cfunc;
|
||||
case LUA_TLCF: /* light C function */
|
||||
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;
|
||||
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);
|
||||
|
||||
171
ldump.c
171
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,147 +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(f->upvalues[i].kind, 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -221,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;
|
||||
}
|
||||
|
||||
|
||||
22
lfunc.c
22
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);
|
||||
@@ -63,7 +63,7 @@ void luaF_initupvals (lua_State *L, LClosure *cl) {
|
||||
** open upvalues of 'L' after entry 'prev'.
|
||||
**/
|
||||
static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) {
|
||||
GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal));
|
||||
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 */
|
||||
@@ -165,7 +165,7 @@ static int callclosemth (lua_State *L, StkId level, int status) {
|
||||
if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */
|
||||
status = newstatus; /* this will be the new error */
|
||||
else {
|
||||
if (newstatus != LUA_OK) /* supressed error? */
|
||||
if (newstatus != LUA_OK) /* suppressed error? */
|
||||
luaE_warnerror(L, "__close metamethod");
|
||||
/* leave original error (or nil) on top */
|
||||
L->top = restorestack(L, oldtop);
|
||||
@@ -243,7 +243,7 @@ int luaF_close (lua_State *L, StkId level, int 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);
|
||||
|
||||
61
lgc.c
61
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,19 +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_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 */
|
||||
@@ -289,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;
|
||||
}
|
||||
@@ -598,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);
|
||||
@@ -710,34 +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_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);
|
||||
@@ -1049,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));
|
||||
@@ -1069,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? */
|
||||
@@ -1565,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);
|
||||
|
||||
5
liolib.c
5
liolib.c
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
2
llex.c
2
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 */
|
||||
|
||||
@@ -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)
|
||||
|
||||
32
lmathlib.c
32
lmathlib.c
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
2
lmem.c
2
lmem.c
@@ -26,7 +26,7 @@
|
||||
/*
|
||||
** 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 alocation.)
|
||||
** 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)
|
||||
|
||||
25
loadlib.c
25
loadlib.c
@@ -67,6 +67,13 @@ static const char *const CLIBS = "_CLIBS";
|
||||
#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
|
||||
@@ -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);
|
||||
@@ -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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
184
lobject.h
184
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) \
|
||||
((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_; \
|
||||
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
|
||||
*/
|
||||
@@ -527,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)
|
||||
|
||||
@@ -549,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); }
|
||||
|
||||
|
||||
@@ -615,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)
|
||||
@@ -692,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)
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "lopcodes.h"
|
||||
|
||||
|
||||
@@ -24,7 +22,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||
,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_LOADBOOL */
|
||||
,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 */
|
||||
|
||||
184
lopcodes.h
184
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
|
||||
@@ -133,7 +133,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
|
||||
#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)
|
||||
|
||||
@@ -183,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]
|
||||
*/
|
||||
|
||||
|
||||
@@ -195,113 +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) := {} */
|
||||
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) + sC */
|
||||
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) >> sC */
|
||||
OP_SHLI,/* A B sC R(A) := sC << 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_MMBIN,/* A B C call C metamethod over R(A) and R(B) */
|
||||
OP_MMBINI,/* A sB C call C metamethod over R(A) and sB */
|
||||
OP_MMBINK,/* A B C call C metamethod over R(A) and K(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_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_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_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */
|
||||
OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
|
||||
|
||||
OP_CLOSE,/* A close all upvalues >= R(A) */
|
||||
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 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;
|
||||
@@ -323,7 +325,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
||||
|
||||
(*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
|
||||
EXTRAARG.
|
||||
OP_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
|
||||
@@ -336,6 +338,9 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
(*) 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
|
||||
@@ -344,7 +349,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||
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.)
|
||||
|
||||
===========================================================================*/
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
10
loslib.c
10
loslib.c
@@ -10,6 +10,7 @@
|
||||
#include "lprefix.h"
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -91,7 +92,7 @@
|
||||
|
||||
/* 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 /* } */
|
||||
|
||||
@@ -138,10 +139,11 @@
|
||||
|
||||
|
||||
|
||||
|
||||
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 {
|
||||
@@ -196,7 +198,7 @@ static int os_clock (lua_State *L) {
|
||||
*/
|
||||
|
||||
/*
|
||||
** About the overflow check: an overflow cannot occurr when time
|
||||
** 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
|
||||
|
||||
73
lparser.c
73
lparser.c
@@ -212,27 +212,28 @@ static int new_localvar (LexState *ls, TString *name) {
|
||||
|
||||
|
||||
/*
|
||||
** 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];
|
||||
static Vardesc *getlocalvardesc (FuncState *fs, int vidx) {
|
||||
return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert 'nvar' (number of active variables at some point) to
|
||||
** number of variables in the stack at that point.
|
||||
** 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 int stacklevel (FuncState *fs, int nvar) {
|
||||
while (nvar > 0) {
|
||||
Vardesc *vd = getlocalvardesc(fs, nvar - 1);
|
||||
while (nvar-- > 0) {
|
||||
Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */
|
||||
if (vd->vd.kind != RDKCTC) /* is in the stack? */
|
||||
return vd->vd.sidx + 1;
|
||||
else
|
||||
nvar--; /* try previous variable */
|
||||
}
|
||||
return 0; /* no variables */
|
||||
return 0; /* no variables in the stack */
|
||||
}
|
||||
|
||||
|
||||
@@ -245,10 +246,10 @@ int luaY_nvarstack (FuncState *fs) {
|
||||
|
||||
|
||||
/*
|
||||
** Get the debug-information entry for current variable 'i'.
|
||||
** Get the debug-information entry for current variable 'vidx'.
|
||||
*/
|
||||
static LocVar *localdebuginfo (FuncState *fs, int i) {
|
||||
Vardesc *vd = getlocalvardesc(fs, i);
|
||||
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 {
|
||||
@@ -259,14 +260,20 @@ static LocVar *localdebuginfo (FuncState *fs, int i) {
|
||||
}
|
||||
|
||||
|
||||
static void init_var (FuncState *fs, expdesc *e, int i) {
|
||||
/*
|
||||
** 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 = i;
|
||||
e->u.var.sidx = getlocalvardesc(fs, i)->vd.sidx;
|
||||
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) {
|
||||
FuncState *fs = ls->fs;
|
||||
TString *varname = NULL; /* to be set if variable is const */
|
||||
@@ -306,8 +313,8 @@ static void adjustlocalvars (LexState *ls, int nvars) {
|
||||
int stklevel = luaY_nvarstack(fs);
|
||||
int i;
|
||||
for (i = 0; i < nvars; i++) {
|
||||
int varidx = fs->nactvar++;
|
||||
Vardesc *var = getlocalvardesc(fs, varidx);
|
||||
int vidx = fs->nactvar++;
|
||||
Vardesc *var = getlocalvardesc(fs, vidx);
|
||||
var->vd.sidx = stklevel++;
|
||||
var->vd.pidx = registerlocalvar(ls, fs, var->vd.name);
|
||||
}
|
||||
@@ -377,7 +384,8 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
|
||||
|
||||
/*
|
||||
** 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, expdesc *var) {
|
||||
int i;
|
||||
@@ -729,6 +737,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
|
||||
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);
|
||||
}
|
||||
@@ -1014,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;
|
||||
@@ -1591,7 +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, luaY_nvarstack(fs)); /* state may create an upvalue */
|
||||
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);
|
||||
}
|
||||
@@ -1729,7 +1739,7 @@ static int getlocalattribute (LexState *ls) {
|
||||
luaK_semerror(ls,
|
||||
luaO_pushfstring(ls->L, "unknown attribute '%s'", attr));
|
||||
}
|
||||
return VDKREG;
|
||||
return VDKREG; /* regular variable */
|
||||
}
|
||||
|
||||
|
||||
@@ -1738,7 +1748,7 @@ static void checktoclose (LexState *ls, int level) {
|
||||
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, level, 0, 0);
|
||||
luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1748,18 +1758,18 @@ static void localstat (LexState *ls) {
|
||||
FuncState *fs = ls->fs;
|
||||
int toclose = -1; /* index of to-be-closed variable (if any) */
|
||||
Vardesc *var; /* last variable */
|
||||
int ivar, kind; /* index and kind of last variable */
|
||||
int vidx, kind; /* index and kind of last variable */
|
||||
int nvars = 0;
|
||||
int nexps;
|
||||
expdesc e;
|
||||
do {
|
||||
ivar = new_localvar(ls, str_checkname(ls));
|
||||
vidx = new_localvar(ls, str_checkname(ls));
|
||||
kind = getlocalattribute(ls);
|
||||
getlocalvardesc(fs, ivar)->vd.kind = kind;
|
||||
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 = luaY_nvarstack(fs) + nvars;
|
||||
toclose = fs->nactvar + nvars;
|
||||
}
|
||||
nvars++;
|
||||
} while (testnext(ls, ','));
|
||||
@@ -1769,7 +1779,7 @@ static void localstat (LexState *ls) {
|
||||
e.k = VVOID;
|
||||
nexps = 0;
|
||||
}
|
||||
var = getlocalvardesc(fs, ivar); /* get last variable */
|
||||
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? */
|
||||
@@ -1821,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 */
|
||||
}
|
||||
}
|
||||
@@ -1949,6 +1960,7 @@ static void mainfunc (LexState *ls, FuncState *fs) {
|
||||
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);
|
||||
@@ -1967,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;
|
||||
|
||||
@@ -35,7 +35,7 @@ typedef enum {
|
||||
(string is fixed by the lexer) */
|
||||
VNONRELOC, /* expression has its value in a fixed register;
|
||||
info = result register */
|
||||
VLOCAL, /* local variable; var.ridx = local register;
|
||||
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' */
|
||||
@@ -77,7 +77,7 @@ typedef struct expdesc {
|
||||
} ind;
|
||||
struct { /* for local variables */
|
||||
lu_byte sidx; /* index in the stack */
|
||||
unsigned short vidx; /* index in 'actvar.arr' */
|
||||
unsigned short vidx; /* compiler index (in 'actvar.arr') */
|
||||
} var;
|
||||
} u;
|
||||
int t; /* patch list of 'exit when true' */
|
||||
@@ -125,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;
|
||||
|
||||
@@ -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 */
|
||||
|
||||
42
lstate.c
42
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
|
||||
@@ -186,19 +186,26 @@ void luaE_freeCI (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** 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 */
|
||||
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 there are two nexts */
|
||||
while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
|
||||
luaM_free(L, ci->next); /* free next */
|
||||
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 */
|
||||
}
|
||||
@@ -286,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 = CSTACKTHREAD;
|
||||
L->hook = NULL;
|
||||
L->hookmask = 0;
|
||||
L->basehookcount = 0;
|
||||
@@ -319,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);
|
||||
@@ -327,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;
|
||||
@@ -355,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;
|
||||
@@ -382,7 +388,7 @@ 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);
|
||||
|
||||
18
lstate.h
18
lstate.h
@@ -173,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 */
|
||||
@@ -300,7 +300,7 @@ struct lua_State {
|
||||
int stacksize;
|
||||
int basehookcount;
|
||||
int hookcount;
|
||||
l_signalT hookmask;
|
||||
volatile l_signalT hookmask;
|
||||
};
|
||||
|
||||
|
||||
@@ -327,15 +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((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))
|
||||
|
||||
|
||||
/*
|
||||
|
||||
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);
|
||||
|
||||
51
lstrlib.c
51
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;
|
||||
}
|
||||
|
||||
@@ -988,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)
|
||||
|
||||
|
||||
/*
|
||||
@@ -1056,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))
|
||||
|
||||
|
||||
/*
|
||||
@@ -1071,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")
|
||||
@@ -1169,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) */
|
||||
@@ -1247,11 +1266,15 @@ static int str_format (lua_State *L) {
|
||||
case 'e': case 'E': case 'g': case 'G': {
|
||||
lua_Number n = luaL_checknumber(L, arg);
|
||||
addlenmod(form, LUA_NUMBER_FRMLEN);
|
||||
nb = snprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
|
||||
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;
|
||||
}
|
||||
|
||||
57
ltable.c
57
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 */
|
||||
|
||||
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);
|
||||
|
||||
67
ltests.c
67
ltests.c
@@ -73,14 +73,16 @@ static void badexit (const char *fmt, const char *s1, const char *s2) {
|
||||
|
||||
|
||||
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), NULL),
|
||||
msg, NULL),
|
||||
0); /* do not return to Lua */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Warning function for tests. Fist, it concatenates all parts of
|
||||
** Warning function for tests. First, it concatenates all parts of
|
||||
** a warning in buffer 'buff'. Then, it has three modes:
|
||||
** - 0.normal: messages starting with '#' are shown on standard output;
|
||||
** - other messages abort the tests (they represent real warning
|
||||
@@ -131,8 +133,7 @@ static void warnf (void *ud, const char *msg, int tocont) {
|
||||
if (buff[0] != '#' && onoff) /* unexpected warning? */
|
||||
badexit("Unexpected warning in test mode: %s\naborting...\n",
|
||||
buff, NULL);
|
||||
/* else */ /* FALLTHROUGH */
|
||||
}
|
||||
} /* FALLTHROUGH */
|
||||
case 1: { /* allow */
|
||||
if (onoff)
|
||||
fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
|
||||
@@ -303,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)));
|
||||
}
|
||||
|
||||
@@ -419,52 +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_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;
|
||||
}
|
||||
@@ -476,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').
|
||||
@@ -497,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);
|
||||
@@ -511,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 */
|
||||
}
|
||||
}
|
||||
@@ -570,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 */
|
||||
@@ -584,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;
|
||||
}
|
||||
@@ -783,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
ltests.h
19
ltests.h
@@ -25,9 +25,6 @@
|
||||
#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
|
||||
@@ -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
|
||||
|
||||
|
||||
11
ltm.c
11
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",
|
||||
@@ -188,6 +188,15 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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 */
|
||||
|
||||
2
ltm.h
2
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);
|
||||
|
||||
3
lua.c
3
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
8
lua.h
8
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
|
||||
|
||||
/* }============================================================== */
|
||||
|
||||
/*
|
||||
@@ -489,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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
182
lundump.c
182
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;
|
||||
f->upvalues[i].instack = LoadByte(S);
|
||||
f->upvalues[i].idx = LoadByte(S);
|
||||
f->upvalues[i].kind = LoadByte(S);
|
||||
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 */
|
||||
|
||||
12
lutf8lib.c
12
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,9 +97,9 @@ 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? */
|
||||
@@ -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--;
|
||||
|
||||
379
lvm.c
379
lvm.c
@@ -55,7 +55,7 @@
|
||||
*/
|
||||
|
||||
/* number of bits in the mantissa of a float */
|
||||
#define NBM (l_mathlim(MANT_DIG))
|
||||
#define NBM (l_floatatt(MANT_DIG))
|
||||
|
||||
/*
|
||||
** Check whether some integers may not fit in a float, testing whether
|
||||
@@ -80,6 +80,21 @@
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Try to convert a value from string to a number value.
|
||||
** If the value is not a string or is a string not representing
|
||||
** a valid numeral (or if coercions from strings to numbers
|
||||
** are disabled via macro 'cvt2num'), do not modify 'result'
|
||||
** and return 0.
|
||||
*/
|
||||
static int l_strton (const TValue *obj, TValue *result) {
|
||||
lua_assert(obj != result);
|
||||
if (!cvt2num(obj)) /* is object not a string? */
|
||||
return 0;
|
||||
else
|
||||
return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Try to convert a value to a float. The float case is already handled
|
||||
@@ -91,8 +106,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
|
||||
*n = cast_num(ivalue(obj));
|
||||
return 1;
|
||||
}
|
||||
else if (cvt2num(obj) && /* string coercible to number? */
|
||||
luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) {
|
||||
else if (l_strton(obj, &v)) { /* string coercible to number? */
|
||||
*n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
|
||||
return 1;
|
||||
}
|
||||
@@ -102,16 +116,13 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
|
||||
|
||||
|
||||
/*
|
||||
** try to convert a float to an integer, rounding according to 'mode':
|
||||
** mode == 0: accepts only integral values
|
||||
** mode == 1: takes the floor of the number
|
||||
** mode == 2: takes the ceil of the number
|
||||
** try to convert a float to an integer, rounding according to 'mode'.
|
||||
*/
|
||||
int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
|
||||
int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
|
||||
lua_Number f = l_floor(n);
|
||||
if (n != f) { /* not an integral value? */
|
||||
if (mode == 0) return 0; /* fails if mode demands integral value */
|
||||
else if (mode > 1) /* needs ceil? */
|
||||
if (mode == F2Ieq) return 0; /* fails if mode demands integral value */
|
||||
else if (mode == F2Iceil) /* needs ceil? */
|
||||
f += 1; /* convert floor to ceil (remember: n != f) */
|
||||
}
|
||||
return lua_numbertointeger(f, p);
|
||||
@@ -123,7 +134,7 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
|
||||
** without string coercion.
|
||||
** ("Fast track" handled by macro 'tointegerns'.)
|
||||
*/
|
||||
int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) {
|
||||
int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
|
||||
if (ttisfloat(obj))
|
||||
return luaV_flttointeger(fltvalue(obj), p, mode);
|
||||
else if (ttisinteger(obj)) {
|
||||
@@ -138,33 +149,33 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) {
|
||||
/*
|
||||
** try to convert a value to an integer.
|
||||
*/
|
||||
int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
|
||||
int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
|
||||
TValue v;
|
||||
if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1)
|
||||
obj = &v; /* change string to its corresponding number */
|
||||
if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
|
||||
obj = &v; /* change it to point to its corresponding number */
|
||||
return luaV_tointegerns(obj, p, mode);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Try to convert a 'for' limit to an integer, preserving the semantics
|
||||
** of the loop. (The following explanation assumes a positive step;
|
||||
** it is valid for negative steps mutatis mutandis.)
|
||||
** Return true if the loop must not run.
|
||||
** of the loop. Return true if the loop must not run; otherwise, '*p'
|
||||
** gets the integer limit.
|
||||
** (The following explanation assumes a positive step; it is valid for
|
||||
** negative steps mutatis mutandis.)
|
||||
** If the limit is an integer or can be converted to an integer,
|
||||
** rounding down, that is the limit.
|
||||
** Otherwise, check whether the limit can be converted to a float. If
|
||||
** the float is too large, clip it to LUA_MAXINTEGER. If the float
|
||||
** is too negative, the loop should not run, because any initial
|
||||
** integer value is greater than such limit; so, it returns true to
|
||||
** signal that.
|
||||
** (For this latter case, no integer limit would be correct; even a
|
||||
** limit of LUA_MININTEGER would run the loop once for an initial
|
||||
** value equal to LUA_MININTEGER.)
|
||||
** integer value is greater than such limit; so, the function returns
|
||||
** true to signal that. (For this latter case, no integer limit would be
|
||||
** correct; even a limit of LUA_MININTEGER would run the loop once for
|
||||
** an initial value equal to LUA_MININTEGER.)
|
||||
*/
|
||||
static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
|
||||
lua_Integer *p, lua_Integer step) {
|
||||
if (!luaV_tointeger(lim, p, (step < 0 ? 2 : 1))) {
|
||||
if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
|
||||
/* not coercible to in integer */
|
||||
lua_Number flim; /* try to convert to float */
|
||||
if (!tonumber(lim, &flim)) /* cannot convert to float? */
|
||||
@@ -183,6 +194,91 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Prepare a numerical for loop (opcode OP_FORPREP).
|
||||
** Return true to skip the loop. Otherwise,
|
||||
** after preparation, stack will be as follows:
|
||||
** ra : internal index (safe copy of the control variable)
|
||||
** ra + 1 : loop counter (integer loops) or limit (float loops)
|
||||
** ra + 2 : step
|
||||
** ra + 3 : control variable
|
||||
*/
|
||||
static int forprep (lua_State *L, StkId ra) {
|
||||
TValue *pinit = s2v(ra);
|
||||
TValue *plimit = s2v(ra + 1);
|
||||
TValue *pstep = s2v(ra + 2);
|
||||
if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
|
||||
lua_Integer init = ivalue(pinit);
|
||||
lua_Integer step = ivalue(pstep);
|
||||
lua_Integer limit;
|
||||
if (step == 0)
|
||||
luaG_runerror(L, "'for' step is zero");
|
||||
setivalue(s2v(ra + 3), init); /* control variable */
|
||||
if (forlimit(L, init, plimit, &limit, step))
|
||||
return 1; /* skip the loop */
|
||||
else { /* prepare loop counter */
|
||||
lua_Unsigned count;
|
||||
if (step > 0) { /* ascending loop? */
|
||||
count = l_castS2U(limit) - l_castS2U(init);
|
||||
if (step != 1) /* avoid division in the too common case */
|
||||
count /= l_castS2U(step);
|
||||
}
|
||||
else { /* step < 0; descending loop */
|
||||
count = l_castS2U(init) - l_castS2U(limit);
|
||||
/* 'step+1' avoids negating 'mininteger' */
|
||||
count /= l_castS2U(-(step + 1)) + 1u;
|
||||
}
|
||||
/* store the counter in place of the limit (which won't be
|
||||
needed anymore */
|
||||
setivalue(plimit, l_castU2S(count));
|
||||
}
|
||||
}
|
||||
else { /* try making all values floats */
|
||||
lua_Number init; lua_Number limit; lua_Number step;
|
||||
if (unlikely(!tonumber(plimit, &limit)))
|
||||
luaG_forerror(L, plimit, "limit");
|
||||
if (unlikely(!tonumber(pstep, &step)))
|
||||
luaG_forerror(L, pstep, "step");
|
||||
if (unlikely(!tonumber(pinit, &init)))
|
||||
luaG_forerror(L, pinit, "initial value");
|
||||
if (step == 0)
|
||||
luaG_runerror(L, "'for' step is zero");
|
||||
if (luai_numlt(0, step) ? luai_numlt(limit, init)
|
||||
: luai_numlt(init, limit))
|
||||
return 1; /* skip the loop */
|
||||
else {
|
||||
/* make sure internal values are all floats */
|
||||
setfltvalue(plimit, limit);
|
||||
setfltvalue(pstep, step);
|
||||
setfltvalue(s2v(ra), init); /* internal index */
|
||||
setfltvalue(s2v(ra + 3), init); /* control variable */
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a step of a float numerical for loop, returning
|
||||
** true iff the loop must continue. (The integer case is
|
||||
** written online with opcode OP_FORLOOP, for performance.)
|
||||
*/
|
||||
static int floatforloop (StkId ra) {
|
||||
lua_Number step = fltvalue(s2v(ra + 2));
|
||||
lua_Number limit = fltvalue(s2v(ra + 1));
|
||||
lua_Number idx = fltvalue(s2v(ra)); /* internal index */
|
||||
idx = luai_numadd(L, idx, step); /* increment index */
|
||||
if (luai_numlt(0, step) ? luai_numle(idx, limit)
|
||||
: luai_numle(limit, idx)) {
|
||||
chgfltvalue(s2v(ra), idx); /* update internal index */
|
||||
setfltvalue(s2v(ra + 3), idx); /* and control variable */
|
||||
return 1; /* jump back */
|
||||
}
|
||||
else
|
||||
return 0; /* finish the loop */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Finish the table access 'val = t[key]'.
|
||||
** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to
|
||||
@@ -318,7 +414,7 @@ static int LTintfloat (lua_Integer i, lua_Number f) {
|
||||
return luai_numlt(cast_num(i), f); /* compare them as floats */
|
||||
else { /* i < f <=> i < ceil(f) */
|
||||
lua_Integer fi;
|
||||
if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */
|
||||
if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
|
||||
return i < fi; /* compare them as integers */
|
||||
else /* 'f' is either greater or less than all integers */
|
||||
return f > 0; /* greater? */
|
||||
@@ -335,7 +431,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) {
|
||||
return luai_numle(cast_num(i), f); /* compare them as floats */
|
||||
else { /* i <= f <=> i <= floor(f) */
|
||||
lua_Integer fi;
|
||||
if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */
|
||||
if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
|
||||
return i <= fi; /* compare them as integers */
|
||||
else /* 'f' is either greater or less than all integers */
|
||||
return f > 0; /* greater? */
|
||||
@@ -352,7 +448,7 @@ static int LTfloatint (lua_Number f, lua_Integer i) {
|
||||
return luai_numlt(f, cast_num(i)); /* compare them as floats */
|
||||
else { /* f < i <=> floor(f) < i */
|
||||
lua_Integer fi;
|
||||
if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */
|
||||
if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
|
||||
return fi < i; /* compare them as integers */
|
||||
else /* 'f' is either greater or less than all integers */
|
||||
return f < 0; /* less? */
|
||||
@@ -369,7 +465,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) {
|
||||
return luai_numle(f, cast_num(i)); /* compare them as floats */
|
||||
else { /* f <= i <=> ceil(f) <= i */
|
||||
lua_Integer fi;
|
||||
if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */
|
||||
if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
|
||||
return fi <= i; /* compare them as integers */
|
||||
else /* 'f' is either greater or less than all integers */
|
||||
return f < 0; /* less? */
|
||||
@@ -445,11 +541,6 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
|
||||
|
||||
/*
|
||||
** return 'l <= r' for non-numbers.
|
||||
** If it needs a metamethod and 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.
|
||||
*/
|
||||
static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
|
||||
lua_assert(!ttisnumber(l) || !ttisnumber(r));
|
||||
@@ -486,15 +577,14 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
|
||||
}
|
||||
/* values have same type and same variant */
|
||||
switch (ttypetag(t1)) {
|
||||
case LUA_TNIL: return 1;
|
||||
case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2));
|
||||
case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
|
||||
case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1! */
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
|
||||
case LUA_TLCF: return fvalue(t1) == fvalue(t2);
|
||||
case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
|
||||
case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
|
||||
case LUA_TUSERDATA: {
|
||||
case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1;
|
||||
case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2));
|
||||
case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
|
||||
case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
|
||||
case LUA_VLCF: return fvalue(t1) == fvalue(t2);
|
||||
case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
|
||||
case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
|
||||
case LUA_VUSERDATA: {
|
||||
if (uvalue(t1) == uvalue(t2)) return 1;
|
||||
else if (L == NULL) return 0;
|
||||
tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
|
||||
@@ -502,7 +592,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
|
||||
tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
|
||||
break; /* will try TM */
|
||||
}
|
||||
case LUA_TTABLE: {
|
||||
case LUA_VTABLE: {
|
||||
if (hvalue(t1) == hvalue(t2)) return 1;
|
||||
else if (L == NULL) return 0;
|
||||
tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
|
||||
@@ -590,18 +680,18 @@ void luaV_concat (lua_State *L, int total) {
|
||||
void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
|
||||
const TValue *tm;
|
||||
switch (ttypetag(rb)) {
|
||||
case LUA_TTABLE: {
|
||||
case LUA_VTABLE: {
|
||||
Table *h = hvalue(rb);
|
||||
tm = fasttm(L, h->metatable, TM_LEN);
|
||||
if (tm) break; /* metamethod? break switch to call it */
|
||||
setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
|
||||
return;
|
||||
}
|
||||
case LUA_TSHRSTR: {
|
||||
case LUA_VSHRSTR: {
|
||||
setivalue(s2v(ra), tsvalue(rb)->shrlen);
|
||||
return;
|
||||
}
|
||||
case LUA_TLNGSTR: {
|
||||
case LUA_VLNGSTR: {
|
||||
setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
|
||||
return;
|
||||
}
|
||||
@@ -788,39 +878,22 @@ void luaV_finishOp (lua_State *L) {
|
||||
#define l_gei(a,b) (a >= b)
|
||||
|
||||
|
||||
/*
|
||||
** Auxiliary macro for arithmetic operations over floats and others
|
||||
** with immediate operand. 'fop' is the float operation; 'tm' is the
|
||||
** corresponding metamethod; 'flip' is true if operands were flipped.
|
||||
*/
|
||||
#define op_arithfI_aux(L,v1,imm,fop,tm,flip) { \
|
||||
lua_Number nb; \
|
||||
if (tonumberns(v1, nb)) { \
|
||||
lua_Number fimm = cast_num(imm); \
|
||||
pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
|
||||
}}
|
||||
|
||||
|
||||
/*
|
||||
** Arithmetic operations over floats and others with immediate operand.
|
||||
*/
|
||||
#define op_arithfI(L,fop,tm) { \
|
||||
TValue *v1 = vRB(i); \
|
||||
int imm = GETARG_sC(i); \
|
||||
op_arithfI_aux(L, v1, imm, fop, tm, 0); }
|
||||
|
||||
/*
|
||||
** Arithmetic operations with immediate operands. 'iop' is the integer
|
||||
** operation.
|
||||
** operation, 'fop' is the float operation.
|
||||
*/
|
||||
#define op_arithI(L,iop,fop,tm,flip) { \
|
||||
#define op_arithI(L,iop,fop) { \
|
||||
TValue *v1 = vRB(i); \
|
||||
int imm = GETARG_sC(i); \
|
||||
if (ttisinteger(v1)) { \
|
||||
lua_Integer iv1 = ivalue(v1); \
|
||||
pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \
|
||||
} \
|
||||
else op_arithfI_aux(L, v1, imm, fop, tm, flip); }
|
||||
else if (ttisfloat(v1)) { \
|
||||
lua_Number nb = fltvalue(v1); \
|
||||
lua_Number fimm = cast_num(imm); \
|
||||
pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
|
||||
}}
|
||||
|
||||
|
||||
/*
|
||||
@@ -844,11 +917,18 @@ void luaV_finishOp (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** Arithmetic operations with register operands.
|
||||
** Arithmetic operations with K operands for floats.
|
||||
*/
|
||||
#define op_arith(L,iop,fop) { \
|
||||
#define op_arithfK(L,fop) { \
|
||||
TValue *v1 = vRB(i); \
|
||||
TValue *v2 = vRC(i); \
|
||||
TValue *v2 = KC(i); \
|
||||
op_arithf_aux(L, v1, v2, fop); }
|
||||
|
||||
|
||||
/*
|
||||
** Arithmetic operations over integers and floats.
|
||||
*/
|
||||
#define op_arith_aux(L,v1,v2,iop,fop) { \
|
||||
if (ttisinteger(v1) && ttisinteger(v2)) { \
|
||||
lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
|
||||
pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
|
||||
@@ -857,32 +937,21 @@ void luaV_finishOp (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** Arithmetic operations with K operands.
|
||||
** Arithmetic operations with register operands.
|
||||
*/
|
||||
#define op_arithK(L,iop,fop,flip) { \
|
||||
#define op_arith(L,iop,fop) { \
|
||||
TValue *v1 = vRB(i); \
|
||||
TValue *v2 = KC(i); \
|
||||
if (ttisinteger(v1) && ttisinteger(v2)) { \
|
||||
lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
|
||||
pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
|
||||
} \
|
||||
else { \
|
||||
lua_Number n1; lua_Number n2; \
|
||||
if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
|
||||
pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
|
||||
}}}
|
||||
TValue *v2 = vRC(i); \
|
||||
op_arith_aux(L, v1, v2, iop, fop); }
|
||||
|
||||
|
||||
/*
|
||||
** Arithmetic operations with K operands for floats.
|
||||
** Arithmetic operations with K operands.
|
||||
*/
|
||||
#define op_arithfK(L,fop) { \
|
||||
#define op_arithK(L,iop,fop) { \
|
||||
TValue *v1 = vRB(i); \
|
||||
TValue *v2 = KC(i); \
|
||||
lua_Number n1; lua_Number n2; \
|
||||
if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
|
||||
pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
|
||||
}}
|
||||
op_arith_aux(L, v1, v2, iop, fop); }
|
||||
|
||||
|
||||
/*
|
||||
@@ -911,9 +980,11 @@ void luaV_finishOp (lua_State *L) {
|
||||
|
||||
|
||||
/*
|
||||
** Order operations with register operands.
|
||||
** Order operations with register operands. 'opn' actually works
|
||||
** for all numbers, but the fast track improves performance for
|
||||
** integers.
|
||||
*/
|
||||
#define op_order(L,opi,opf,other) { \
|
||||
#define op_order(L,opi,opn,other) { \
|
||||
int cond; \
|
||||
TValue *rb = vRB(i); \
|
||||
if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \
|
||||
@@ -922,14 +993,15 @@ void luaV_finishOp (lua_State *L) {
|
||||
cond = opi(ia, ib); \
|
||||
} \
|
||||
else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \
|
||||
cond = opf(s2v(ra), rb); \
|
||||
cond = opn(s2v(ra), rb); \
|
||||
else \
|
||||
Protect(cond = other(L, s2v(ra), rb)); \
|
||||
docondjump(); }
|
||||
|
||||
|
||||
/*
|
||||
** Order operations with immediate operand.
|
||||
** Order operations with immediate operand. (Immediate operand is
|
||||
** always small enough to have an exact representation as a float.)
|
||||
*/
|
||||
#define op_orderI(L,opi,opf,inv,tm) { \
|
||||
int cond; \
|
||||
@@ -1082,7 +1154,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
lua_assert(base == ci->func + 1);
|
||||
lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
|
||||
/* invalidate top for instructions not expecting it */
|
||||
lua_assert(isIT(i) || (L->top = base));
|
||||
lua_assert(isIT(i) || (cast_void(L->top = base), 1));
|
||||
vmdispatch (GET_OPCODE(i)) {
|
||||
vmcase(OP_MOVE) {
|
||||
setobjs2s(L, ra, RB(i));
|
||||
@@ -1109,9 +1181,17 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
setobj2s(L, ra, rb);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_LOADBOOL) {
|
||||
setbvalue(s2v(ra), GETARG_B(i));
|
||||
if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
|
||||
vmcase(OP_LOADFALSE) {
|
||||
setbfvalue(s2v(ra));
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_LFALSESKIP) {
|
||||
setbfvalue(s2v(ra));
|
||||
pc++; /* skip next instruction */
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_LOADTRUE) {
|
||||
setbtvalue(s2v(ra));
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_LOADNIL) {
|
||||
@@ -1243,8 +1323,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
Table *t;
|
||||
if (b > 0)
|
||||
b = 1 << (b - 1); /* size is 2^(b - 1) */
|
||||
if (TESTARG_k(i))
|
||||
c += GETARG_Ax(*pc) * (MAXARG_C + 1);
|
||||
lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0));
|
||||
if (TESTARG_k(i)) /* non-zero extra argument? */
|
||||
c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */
|
||||
pc++; /* skip extra argument */
|
||||
L->top = ra + 1; /* correct top in case of emergency GC */
|
||||
t = luaH_new(L); /* memory allocation */
|
||||
@@ -1268,23 +1349,23 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_ADDI) {
|
||||
op_arithI(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i));
|
||||
op_arithI(L, l_addi, luai_numadd);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_ADDK) {
|
||||
op_arithK(L, l_addi, luai_numadd, GETARG_k(i));
|
||||
op_arithK(L, l_addi, luai_numadd);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_SUBK) {
|
||||
op_arithK(L, l_subi, luai_numsub, 0);
|
||||
op_arithK(L, l_subi, luai_numsub);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_MULK) {
|
||||
op_arithK(L, l_muli, luai_nummul, GETARG_k(i));
|
||||
op_arithK(L, l_muli, luai_nummul);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_MODK) {
|
||||
op_arithK(L, luaV_mod, luaV_modf, 0);
|
||||
op_arithK(L, luaV_mod, luaV_modf);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_POWK) {
|
||||
@@ -1296,7 +1377,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_IDIVK) {
|
||||
op_arithK(L, luaV_idiv, luai_numidiv, 0);
|
||||
op_arithK(L, luaV_idiv, luai_numidiv);
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_BANDK) {
|
||||
@@ -1430,8 +1511,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
}
|
||||
vmcase(OP_NOT) {
|
||||
TValue *rb = vRB(i);
|
||||
int nrb = l_isfalse(rb); /* next assignment may change this value */
|
||||
setbvalue(s2v(ra), nrb);
|
||||
if (l_isfalse(rb))
|
||||
setbtvalue(s2v(ra));
|
||||
else
|
||||
setbfvalue(s2v(ra));
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_LEN) {
|
||||
@@ -1476,7 +1559,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
vmcase(OP_EQK) {
|
||||
TValue *rb = KB(i);
|
||||
/* basic types do not use '__eq'; we can use raw equality */
|
||||
int cond = luaV_equalobj(NULL, s2v(ra), rb);
|
||||
int cond = luaV_rawequalobj(s2v(ra), rb);
|
||||
docondjump();
|
||||
vmbreak;
|
||||
}
|
||||
@@ -1549,9 +1632,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
luaF_close(L, base, NOCLOSINGMETH);
|
||||
lua_assert(base == ci->func + 1);
|
||||
}
|
||||
if (!ttisfunction(s2v(ra))) { /* not a function? */
|
||||
while (!ttisfunction(s2v(ra))) { /* not a function? */
|
||||
luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
|
||||
b++; /* there is now one extra argument */
|
||||
checkstackp(L, 1, ra);
|
||||
}
|
||||
if (!ttisLclosure(s2v(ra))) { /* C function? */
|
||||
luaD_call(L, ra, LUA_MULTRET); /* call it */
|
||||
@@ -1561,12 +1645,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
luaD_poscall(L, ci, cast_int(L->top - ra));
|
||||
return;
|
||||
}
|
||||
else { /* Lua tail call */
|
||||
ci->func -= delta;
|
||||
luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
|
||||
goto tailcall;
|
||||
}
|
||||
vmbreak;
|
||||
ci->func -= delta;
|
||||
luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
|
||||
goto tailcall;
|
||||
}
|
||||
vmcase(OP_RETURN) {
|
||||
int n = GETARG_B(i) - 1; /* number of results */
|
||||
@@ -1633,73 +1714,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
||||
pc -= GETARG_Bx(i); /* jump back */
|
||||
}
|
||||
}
|
||||
else { /* floating loop */
|
||||
lua_Number step = fltvalue(s2v(ra + 2));
|
||||
lua_Number limit = fltvalue(s2v(ra + 1));
|
||||
lua_Number idx = fltvalue(s2v(ra));
|
||||
idx = luai_numadd(L, idx, step); /* increment index */
|
||||
if (luai_numlt(0, step) ? luai_numle(idx, limit)
|
||||
: luai_numle(limit, idx)) {
|
||||
chgfltvalue(s2v(ra), idx); /* update internal index */
|
||||
setfltvalue(s2v(ra + 3), idx); /* and control variable */
|
||||
pc -= GETARG_Bx(i); /* jump back */
|
||||
}
|
||||
}
|
||||
else if (floatforloop(ra)) /* float loop */
|
||||
pc -= GETARG_Bx(i); /* jump back */
|
||||
updatetrap(ci); /* allows a signal to break the loop */
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_FORPREP) {
|
||||
TValue *pinit = s2v(ra);
|
||||
TValue *plimit = s2v(ra + 1);
|
||||
TValue *pstep = s2v(ra + 2);
|
||||
savestate(L, ci); /* in case of errors */
|
||||
if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
|
||||
lua_Integer init = ivalue(pinit);
|
||||
lua_Integer step = ivalue(pstep);
|
||||
lua_Integer limit;
|
||||
if (step == 0)
|
||||
luaG_runerror(L, "'for' step is zero");
|
||||
setivalue(s2v(ra + 3), init); /* control variable */
|
||||
if (forlimit(L, init, plimit, &limit, step))
|
||||
pc += GETARG_Bx(i) + 1; /* skip the loop */
|
||||
else { /* prepare loop counter */
|
||||
lua_Unsigned count;
|
||||
if (step > 0) { /* ascending loop? */
|
||||
count = l_castS2U(limit) - l_castS2U(init);
|
||||
if (step != 1) /* avoid division in the too common case */
|
||||
count /= l_castS2U(step);
|
||||
}
|
||||
else { /* step < 0; descending loop */
|
||||
count = l_castS2U(init) - l_castS2U(limit);
|
||||
/* 'step+1' avoids negating 'mininteger' */
|
||||
count /= l_castS2U(-(step + 1)) + 1u;
|
||||
}
|
||||
/* store the counter in place of the limit (which won't be
|
||||
needed anymore */
|
||||
setivalue(plimit, l_castU2S(count));
|
||||
}
|
||||
}
|
||||
else { /* try making all values floats */
|
||||
lua_Number init; lua_Number limit; lua_Number step;
|
||||
if (unlikely(!tonumber(plimit, &limit)))
|
||||
luaG_forerror(L, plimit, "limit");
|
||||
if (unlikely(!tonumber(pstep, &step)))
|
||||
luaG_forerror(L, pstep, "step");
|
||||
if (unlikely(!tonumber(pinit, &init)))
|
||||
luaG_forerror(L, pinit, "initial value");
|
||||
if (step == 0)
|
||||
luaG_runerror(L, "'for' step is zero");
|
||||
if (luai_numlt(0, step) ? luai_numlt(limit, init)
|
||||
: luai_numlt(init, limit))
|
||||
pc += GETARG_Bx(i) + 1; /* skip the loop */
|
||||
else {
|
||||
/* make sure internal values are all float */
|
||||
setfltvalue(plimit, limit);
|
||||
setfltvalue(pstep, step);
|
||||
setfltvalue(s2v(ra), init); /* internal index */
|
||||
setfltvalue(s2v(ra + 3), init); /* control variable */
|
||||
}
|
||||
}
|
||||
if (forprep(L, ra))
|
||||
pc += GETARG_Bx(i) + 1; /* skip the loop */
|
||||
vmbreak;
|
||||
}
|
||||
vmcase(OP_TFORPREP) {
|
||||
|
||||
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,
|
||||
|
||||
5
makefile
5
makefile
@@ -1,6 +1,5 @@
|
||||
# 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 =======================
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
510
manual/manual.of
510
manual/manual.of
File diff suppressed because it is too large
Load Diff
@@ -516,9 +516,11 @@ 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 -1 1; return 1", a) == 20)
|
||||
local a1, res = T.testC("rawgetp -1 1; return 2", a)
|
||||
assert(a == a1 and res == 20)
|
||||
end
|
||||
|
||||
|
||||
@@ -803,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
|
||||
@@ -1110,7 +1112,7 @@ do
|
||||
-- non-closable value
|
||||
local a, b = pcall(T.makeCfunc[[
|
||||
newtable # create non-closable object
|
||||
toclose -1 # mark it to be closed (shoud raise an error)
|
||||
toclose -1 # mark it to be closed (should raise an error)
|
||||
abort # will not be executed
|
||||
]])
|
||||
assert(a == false and
|
||||
|
||||
@@ -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, "..")))
|
||||
|
||||
|
||||
@@ -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,6 +150,27 @@ 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('+')
|
||||
|
||||
|
||||
@@ -399,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
|
||||
|
||||
@@ -144,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 kFalse end, 'LOADBOOL', 'RETURN1')
|
||||
check(function () return not not true end, 'LOADBOOL', 'RETURN1')
|
||||
check(function () return not not k3 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 ()
|
||||
@@ -194,7 +194,7 @@ check(function ()
|
||||
local a,b
|
||||
a[kTrue] = false
|
||||
end,
|
||||
'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0')
|
||||
'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0')
|
||||
|
||||
|
||||
-- equalities
|
||||
|
||||
@@ -763,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)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
local debug = require "debug"
|
||||
|
||||
print"testing C-stack overflow detection"
|
||||
print"If this test craches, see its file ('cstack.lua')"
|
||||
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, you can use the function
|
||||
@@ -19,11 +19,15 @@ print"If this test craches, see its file ('cstack.lua')"
|
||||
-- higher than 2_000.
|
||||
|
||||
|
||||
local origlimit = debug.setcstacklimit(400)
|
||||
-- get and print original limit
|
||||
local origlimit <const> = debug.setcstacklimit(400)
|
||||
print("default stack limit: " .. origlimit)
|
||||
|
||||
-- change this value for different limits for this test suite
|
||||
local currentlimit = 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)
|
||||
|
||||
@@ -33,12 +37,14 @@ local function checkerror (msg, 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
|
||||
|
||||
|
||||
@@ -46,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)
|
||||
@@ -102,12 +108,16 @@ 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(400)
|
||||
return debug.setcstacklimit(alterlimit)
|
||||
end)
|
||||
assert(co() == false) -- cannot change C stack inside coroutine
|
||||
assert(not co()) -- cannot change C stack inside coroutine
|
||||
|
||||
local n
|
||||
local function foo () n = n + 1; foo () end
|
||||
@@ -118,26 +128,32 @@ do print("testing changes in C-stack limit")
|
||||
return n
|
||||
end
|
||||
|
||||
assert(debug.setcstacklimit(400) == currentlimit)
|
||||
local lim400 = check()
|
||||
-- a very low limit (given that the several calls to arive here)
|
||||
local lowlimit = 38
|
||||
assert(debug.setcstacklimit(lowlimit) == 400)
|
||||
assert(check() < lowlimit - 30)
|
||||
assert(debug.setcstacklimit(600) == lowlimit)
|
||||
local lim600 = check()
|
||||
assert(lim600 == lim400 + 200)
|
||||
-- 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(400) == 600)
|
||||
assert(check() <= lim400)
|
||||
assert(debug.setcstacklimit(alterlimit) == lowlimit * 2)
|
||||
assert(check() <= limalter)
|
||||
end))
|
||||
|
||||
assert(check() == lim400)
|
||||
assert(debug.setcstacklimit(origlimit) == 400) -- restore original limit
|
||||
assert(check() == limalter)
|
||||
-- restore original limit
|
||||
assert(debug.setcstacklimit(origlimit) == alterlimit)
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -649,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)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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}
|
||||
@@ -325,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})
|
||||
|
||||
@@ -640,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
|
||||
|
||||
@@ -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')
|
||||
@@ -264,6 +264,43 @@ 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
|
||||
|
||||
@@ -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)
|
||||
@@ -960,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
|
||||
@@ -998,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)
|
||||
|
||||
@@ -76,7 +76,7 @@ end
|
||||
|
||||
-- testing constructor sizes
|
||||
local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17,
|
||||
30, 31, 32, 33, 34, 500, 1000}
|
||||
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 {"}
|
||||
|
||||
@@ -158,25 +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", 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
|
||||
|
||||
do -- short strings are internalized
|
||||
local s1 = string.rep("a", 10)
|
||||
local s2 = string.rep("a", 10)
|
||||
local s2 = string.rep("aa", 5)
|
||||
assert(string.format("%p", s1) == string.format("%p", s2))
|
||||
end
|
||||
do -- long strings
|
||||
|
||||
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
|
||||
assert(#string.format("%90p", {}) == 90)
|
||||
end
|
||||
|
||||
x = '"ílo"\n\\'
|
||||
@@ -309,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"))
|
||||
|
||||
@@ -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