This commit is contained in:
iceman1001 2024-09-30 10:36:29 +02:00
parent c3591b9692
commit 71c86f4b21
68 changed files with 18942 additions and 18745 deletions

View file

@ -1157,7 +1157,7 @@ static int hts_select_tag(const lf_hitag_data_t *packet, uint8_t *tx, size_t siz
key_le = *(uint64_t *)packet->key;
uint64_t state = ht2_hitag2_init(reflect48(key_le), reflect32(tag.data.s.uid_le), reflect32(*(uint32_t*)rnd));
uint64_t state = ht2_hitag2_init(reflect48(key_le), reflect32(tag.data.s.uid_le), reflect32(*(uint32_t *)rnd));
uint8_t auth_ks[4];
for (int i = 0; i < 4; i++) {

View file

@ -1257,7 +1257,7 @@ bool SimulateIso14443aInit(uint8_t tagType, uint16_t flags, uint8_t *data, uint8
// copy the iRATs if supplied
if ((flags & RATS_IN_DATA) == RATS_IN_DATA) {
memcpy(rRATS ,iRATs, sizeof(iRATs));
memcpy(rRATS, iRATs, sizeof(iRATs));
// rats len is dictated by the first char of the string, add 2 crc bytes
rRATS_len = (iRATs[0] + 2);
}
@ -3934,11 +3934,11 @@ void SimulateIso14443aTagAID(uint8_t tagType, uint16_t flags, uint8_t *data, uin
BigBuf_free_keep_EM();
// Increased the buffer size to allow for more complex responses
#define DYNAMIC_RESPONSE_BUFFER2_SIZE 512
#define DYNAMIC_MODULATION_BUFFER2_SIZE 1536
#define DYNAMIC_RESPONSE_BUFFER2_SIZE 512
#define DYNAMIC_MODULATION_BUFFER2_SIZE 1536
uint8_t * dynamic_response_buffer2 = BigBuf_calloc(DYNAMIC_RESPONSE_BUFFER2_SIZE);
uint8_t * dynamic_modulation_buffer2 = BigBuf_calloc(DYNAMIC_MODULATION_BUFFER2_SIZE);
uint8_t *dynamic_response_buffer2 = BigBuf_calloc(DYNAMIC_RESPONSE_BUFFER2_SIZE);
uint8_t *dynamic_modulation_buffer2 = BigBuf_calloc(DYNAMIC_MODULATION_BUFFER2_SIZE);
tag_response_info_t dynamic_response_info = {
.response = dynamic_response_buffer2,
.response_n = 0,
@ -4049,7 +4049,7 @@ void SimulateIso14443aTagAID(uint8_t tagType, uint16_t flags, uint8_t *data, uin
// aid len is found as a hex value in receivedCmd[6] (Index Starts at 0)
int aid_len = receivedCmd[6];
uint8_t* recieved_aid = &receivedCmd[7];
uint8_t *recieved_aid = &receivedCmd[7];
// aid enumeration flag
if (enumerate == true) {
@ -4059,7 +4059,7 @@ void SimulateIso14443aTagAID(uint8_t tagType, uint16_t flags, uint8_t *data, uin
if (memcmp(aidFilter, recieved_aid, aid_len) == 0) { // Evaluate the AID sent by the Reader to the AID supplied
// AID Response will be parsed here
memcpy(dynamic_response_info.response + 2 , aidResponse, respondLen + 2);
memcpy(dynamic_response_info.response + 2, aidResponse, respondLen + 2);
dynamic_response_info.response_n = respondLen + 2;
} else { // Any other SELECT FILE command will return with a Not Found
dynamic_response_info.response[2] = 0x6A;
@ -4080,7 +4080,7 @@ void SimulateIso14443aTagAID(uint8_t tagType, uint16_t flags, uint8_t *data, uin
case 0xCA: { // GET DATA
if (sentCount == 0) {
// APDU Command will just be parsed here
memcpy(dynamic_response_info.response + 2 , apduCommand, apduLen + 2);
memcpy(dynamic_response_info.response + 2, apduCommand, apduLen + 2);
dynamic_response_info.response_n = respondLen + 2;
} else {
finished = true;

View file

@ -57,30 +57,27 @@ const char lua_ident[] =
** Convert an acceptable index to a pointer to its respective value.
** Non-valid indices return the special nil value 'G(L)->nilvalue'.
*/
static TValue *index2value (lua_State *L, int idx) {
static TValue *index2value(lua_State *L, int idx) {
CallInfo *ci = L->ci;
if (idx > 0) {
StkId o = ci->func.p + idx;
api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
if (o >= L->top.p) return &G(L)->nilvalue;
else return s2v(o);
}
else if (!ispseudo(idx)) { /* negative index */
} else if (!ispseudo(idx)) { /* negative index */
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
"invalid index");
return s2v(L->top.p + idx);
}
else if (idx == LUA_REGISTRYINDEX)
} else if (idx == LUA_REGISTRYINDEX)
return &G(L)->l_registry;
else { /* upvalues */
idx = LUA_REGISTRYINDEX - idx;
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */
CClosure *func = clCvalue(s2v(ci->func.p));
return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
return (idx <= func->nupvalues) ? &func->upvalue[idx - 1]
: &G(L)->nilvalue;
}
else { /* light C function or Lua function (through a hook)?) */
} else { /* light C function or Lua function (through a hook)?) */
api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
return &G(L)->nilvalue; /* no upvalues */
}
@ -92,14 +89,13 @@ static TValue *index2value (lua_State *L, int idx) {
/*
** Convert a valid actual index (not a pseudo-index) to its address.
*/
l_sinline StkId index2stack (lua_State *L, int idx) {
l_sinline StkId index2stack(lua_State *L, int idx) {
CallInfo *ci = L->ci;
if (idx > 0) {
StkId o = ci->func.p + idx;
api_check(L, o < L->top.p, "invalid index");
return o;
}
else { /* non-positive index */
} else { /* non-positive index */
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
"invalid index");
api_check(L, !ispseudo(idx), "invalid index");
@ -108,7 +104,7 @@ l_sinline StkId index2stack (lua_State *L, int idx) {
}
LUA_API int lua_checkstack (lua_State *L, int n) {
LUA_API int lua_checkstack(lua_State *L, int n) {
int res;
CallInfo *ci;
lua_lock(L);
@ -125,7 +121,7 @@ LUA_API int lua_checkstack (lua_State *L, int n) {
}
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) {
int i;
if (from == to) return;
lua_lock(to);
@ -141,7 +137,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
}
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf) {
lua_CFunction old;
lua_lock(L);
old = G(L)->panic;
@ -151,7 +147,7 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
}
LUA_API lua_Number lua_version (lua_State *L) {
LUA_API lua_Number lua_version(lua_State *L) {
UNUSED(L);
return LUA_VERSION_NUM;
}
@ -166,19 +162,19 @@ LUA_API lua_Number lua_version (lua_State *L) {
/*
** convert an acceptable stack index into an absolute index
*/
LUA_API int lua_absindex (lua_State *L, int idx) {
LUA_API int lua_absindex(lua_State *L, int idx) {
return (idx > 0 || ispseudo(idx))
? idx
: cast_int(L->top.p - L->ci->func.p) + idx;
}
LUA_API int lua_gettop (lua_State *L) {
LUA_API int lua_gettop(lua_State *L) {
return cast_int(L->top.p - (L->ci->func.p + 1));
}
LUA_API void lua_settop (lua_State *L, int idx) {
LUA_API void lua_settop(lua_State *L, int idx) {
CallInfo *ci;
StkId func, newtop;
ptrdiff_t diff; /* difference for new top */
@ -190,9 +186,8 @@ LUA_API void lua_settop (lua_State *L, int idx) {
diff = ((func + 1) + idx) - L->top.p;
for (; diff > 0; diff--)
setnilvalue(s2v(L->top.p++)); /* clear new slots */
}
else {
api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
} else {
api_check(L, -(idx + 1) <= (L->top.p - (func + 1)), "invalid new top");
diff = idx + 1; /* will "subtract" index (as it is negative) */
}
api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot");
@ -206,7 +201,7 @@ LUA_API void lua_settop (lua_State *L, int idx) {
}
LUA_API void lua_closeslot (lua_State *L, int idx) {
LUA_API void lua_closeslot(lua_State *L, int idx) {
StkId level;
lua_lock(L);
level = index2stack(L, idx);
@ -224,7 +219,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
** Note that we move(copy) only the value inside the stack.
** (We do not move additional fields that may exist.)
*/
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
l_sinline void reverse(lua_State *L, StkId from, StkId to) {
for (; from < to; from++, to--) {
TValue temp;
setobj(L, &temp, s2v(from));
@ -238,7 +233,7 @@ l_sinline void reverse (lua_State *L, StkId from, StkId to) {
** Let x = AB, where A is a prefix of length 'n'. Then,
** rotate x n == BA. But BA == (A^r . B^r)^r.
*/
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
LUA_API void lua_rotate(lua_State *L, int idx, int n) {
StkId p, t, m;
lua_lock(L);
t = L->top.p - 1; /* end of stack segment being rotated */
@ -252,7 +247,7 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
}
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
LUA_API void lua_copy(lua_State *L, int fromidx, int toidx) {
TValue *fr, *to;
lua_lock(L);
fr = index2value(L, fromidx);
@ -267,7 +262,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
}
LUA_API void lua_pushvalue (lua_State *L, int idx) {
LUA_API void lua_pushvalue(lua_State *L, int idx) {
lua_lock(L);
setobj2s(L, L->top.p, index2value(L, idx));
api_incr_top(L);
@ -281,58 +276,58 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) {
*/
LUA_API int lua_type (lua_State *L, int idx) {
LUA_API int lua_type(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
}
LUA_API const char *lua_typename (lua_State *L, int t) {
LUA_API const char *lua_typename(lua_State *L, int t) {
UNUSED(L);
api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
return ttypename(t);
}
LUA_API int lua_iscfunction (lua_State *L, int idx) {
LUA_API int lua_iscfunction(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return (ttislcf(o) || (ttisCclosure(o)));
}
LUA_API int lua_isinteger (lua_State *L, int idx) {
LUA_API int lua_isinteger(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return ttisinteger(o);
}
LUA_API int lua_isnumber (lua_State *L, int idx) {
LUA_API int lua_isnumber(lua_State *L, int idx) {
lua_Number n;
const TValue *o = index2value(L, idx);
return tonumber(o, &n);
}
LUA_API int lua_isstring (lua_State *L, int idx) {
LUA_API int lua_isstring(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return (ttisstring(o) || cvt2str(o));
}
LUA_API int lua_isuserdata (lua_State *L, int idx) {
LUA_API int lua_isuserdata(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return (ttisfulluserdata(o) || ttislightuserdata(o));
}
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
LUA_API int lua_rawequal(lua_State *L, int index1, int index2) {
const TValue *o1 = index2value(L, index1);
const TValue *o2 = index2value(L, index2);
return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
}
LUA_API void lua_arith (lua_State *L, int op) {
LUA_API void lua_arith(lua_State *L, int op) {
lua_lock(L);
if (op != LUA_OPUNM && op != LUA_OPBNOT)
api_checknelems(L, 2); /* all other operations expect two operands */
@ -348,7 +343,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
}
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
LUA_API int lua_compare(lua_State *L, int index1, int index2, int op) {
const TValue *o1;
const TValue *o2;
int i = 0;
@ -357,10 +352,17 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
o2 = index2value(L, index2);
if (isvalid(L, o1) && isvalid(L, o2)) {
switch (op) {
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
default: api_check(L, 0, "invalid option");
case LUA_OPEQ:
i = luaV_equalobj(L, o1, o2);
break;
case LUA_OPLT:
i = luaV_lessthan(L, o1, o2);
break;
case LUA_OPLE:
i = luaV_lessequal(L, o1, o2);
break;
default:
api_check(L, 0, "invalid option");
}
}
lua_unlock(L);
@ -368,7 +370,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
}
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
LUA_API size_t lua_stringtonumber(lua_State *L, const char *s) {
size_t sz = luaO_str2num(s, s2v(L->top.p));
if (sz != 0)
api_incr_top(L);
@ -376,7 +378,7 @@ 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_API lua_Number lua_tonumberx(lua_State *L, int idx, int *pisnum) {
lua_Number n = 0;
const TValue *o = index2value(L, idx);
int isnum = tonumber(o, &n);
@ -386,7 +388,7 @@ LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
}
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *pisnum) {
lua_Integer res = 0;
const TValue *o = index2value(L, idx);
int isnum = tointeger(o, &res);
@ -396,13 +398,13 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
}
LUA_API int lua_toboolean (lua_State *L, int idx) {
LUA_API int lua_toboolean(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return !l_isfalse(o);
}
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) {
TValue *o;
lua_lock(L);
o = index2value(L, idx);
@ -423,19 +425,24 @@ 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) {
LUA_API lua_Unsigned lua_rawlen(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
switch (ttypetag(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;
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;
}
}
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
if (ttislcf(o)) return fvalue(o);
else if (ttisCclosure(o))
@ -444,22 +451,25 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
}
l_sinline void *touserdata (const TValue *o) {
l_sinline void *touserdata(const TValue *o) {
switch (ttype(o)) {
case LUA_TUSERDATA: return getudatamem(uvalue(o));
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return NULL;
case LUA_TUSERDATA:
return getudatamem(uvalue(o));
case LUA_TLIGHTUSERDATA:
return pvalue(o);
default:
return NULL;
}
}
LUA_API void *lua_touserdata (lua_State *L, int idx) {
LUA_API void *lua_touserdata(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return touserdata(o);
}
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
LUA_API lua_State *lua_tothread(lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return (!ttisthread(o)) ? NULL : thvalue(o);
}
@ -472,11 +482,13 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
** a 'size_t'. (As the returned pointer is only informative, this
** conversion should not be a problem.)
*/
LUA_API const void *lua_topointer (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_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
case LUA_VLCF:
return cast_voidp(cast_sizet(fvalue(o)));
case LUA_VUSERDATA:
case LUA_VLIGHTUSERDATA:
return touserdata(o);
default: {
if (iscollectable(o))
@ -494,7 +506,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
*/
LUA_API void lua_pushnil (lua_State *L) {
LUA_API void lua_pushnil(lua_State *L) {
lua_lock(L);
setnilvalue(s2v(L->top.p));
api_incr_top(L);
@ -502,7 +514,7 @@ LUA_API void lua_pushnil (lua_State *L) {
}
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
LUA_API void lua_pushnumber(lua_State *L, lua_Number n) {
lua_lock(L);
setfltvalue(s2v(L->top.p), n);
api_incr_top(L);
@ -510,7 +522,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
}
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) {
lua_lock(L);
setivalue(s2v(L->top.p), n);
api_incr_top(L);
@ -523,7 +535,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
** 'len' == 0 (as 's' can be NULL in that case), due to later use of
** 'memcmp' and 'memcpy'.
*/
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
LUA_API const char *lua_pushlstring(lua_State *L, const char *s, size_t len) {
TString *ts;
lua_lock(L);
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
@ -535,7 +547,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
}
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
LUA_API const char *lua_pushstring(lua_State *L, const char *s) {
lua_lock(L);
if (s == NULL)
setnilvalue(s2v(L->top.p));
@ -552,7 +564,7 @@ LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
}
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
va_list argp) {
const char *ret;
lua_lock(L);
@ -563,7 +575,7 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
}
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...) {
const char *ret;
va_list argp;
lua_lock(L);
@ -576,13 +588,12 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
}
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n) {
lua_lock(L);
if (n == 0) {
setfvalue(s2v(L->top.p), fn);
api_incr_top(L);
}
else {
} else {
CClosure *cl;
api_checknelems(L, n);
api_check(L, n <= MAXUPVAL, "upvalue index too large");
@ -602,7 +613,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
}
LUA_API void lua_pushboolean (lua_State *L, int b) {
LUA_API void lua_pushboolean(lua_State *L, int b) {
lua_lock(L);
if (b)
setbtvalue(s2v(L->top.p));
@ -613,7 +624,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) {
}
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
LUA_API void lua_pushlightuserdata(lua_State *L, void *p) {
lua_lock(L);
setpvalue(s2v(L->top.p), p);
api_incr_top(L);
@ -621,7 +632,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
}
LUA_API int lua_pushthread (lua_State *L) {
LUA_API int lua_pushthread(lua_State *L) {
lua_lock(L);
setthvalue(L, s2v(L->top.p), L);
api_incr_top(L);
@ -636,14 +647,13 @@ LUA_API int lua_pushthread (lua_State *L) {
*/
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
l_sinline int auxgetstr(lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
TString *str = luaS_new(L, k);
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
setobj2s(L, L->top.p, slot);
api_incr_top(L);
}
else {
} else {
setsvalue2s(L, L->top.p, str);
api_incr_top(L);
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
@ -663,7 +673,7 @@ l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
(&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
LUA_API int lua_getglobal (lua_State *L, const char *name) {
LUA_API int lua_getglobal(lua_State *L, const char *name) {
const TValue *G;
lua_lock(L);
G = getGtable(L);
@ -671,36 +681,34 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) {
}
LUA_API int lua_gettable (lua_State *L, int idx) {
LUA_API int lua_gettable(lua_State *L, int idx) {
const TValue *slot;
TValue *t;
lua_lock(L);
t = index2value(L, idx);
if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
setobj2s(L, L->top.p - 1, slot);
}
else
} else
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
lua_unlock(L);
return ttype(s2v(L->top.p - 1));
}
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
LUA_API int lua_getfield(lua_State *L, int idx, const char *k) {
lua_lock(L);
return auxgetstr(L, index2value(L, idx), k);
}
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
LUA_API int lua_geti(lua_State *L, int idx, lua_Integer n) {
TValue *t;
const TValue *slot;
lua_lock(L);
t = index2value(L, idx);
if (luaV_fastgeti(L, t, n, slot)) {
setobj2s(L, L->top.p, slot);
}
else {
} else {
TValue aux;
setivalue(&aux, n);
luaV_finishget(L, t, &aux, L->top.p, slot);
@ -711,7 +719,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
}
l_sinline int finishrawget (lua_State *L, const TValue *val) {
l_sinline int finishrawget(lua_State *L, const TValue *val) {
if (isempty(val)) /* avoid copying empty items to the stack */
setnilvalue(s2v(L->top.p));
else
@ -722,14 +730,14 @@ l_sinline int finishrawget (lua_State *L, const TValue *val) {
}
static Table *gettable (lua_State *L, int idx) {
static Table *gettable(lua_State *L, int idx) {
TValue *t = index2value(L, idx);
api_check(L, ttistable(t), "table expected");
return hvalue(t);
}
LUA_API int lua_rawget (lua_State *L, int idx) {
LUA_API int lua_rawget(lua_State *L, int idx) {
Table *t;
const TValue *val;
lua_lock(L);
@ -741,7 +749,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
}
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
LUA_API int lua_rawgeti(lua_State *L, int idx, lua_Integer n) {
Table *t;
lua_lock(L);
t = gettable(L, idx);
@ -749,7 +757,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
}
LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
LUA_API int lua_rawgetp(lua_State *L, int idx, const void *p) {
Table *t;
TValue k;
lua_lock(L);
@ -759,7 +767,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
}
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
LUA_API void lua_createtable(lua_State *L, int narray, int nrec) {
Table *t;
lua_lock(L);
t = luaH_new(L);
@ -772,7 +780,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
}
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
LUA_API int lua_getmetatable(lua_State *L, int objindex) {
const TValue *obj;
Table *mt;
int res = 0;
@ -799,7 +807,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
}
LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
LUA_API int lua_getiuservalue(lua_State *L, int idx, int n) {
TValue *o;
int t;
lua_lock(L);
@ -808,8 +816,7 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
if (n <= 0 || n > uvalue(o)->nuvalue) {
setnilvalue(s2v(L->top.p));
t = LUA_TNONE;
}
else {
} else {
setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
t = ttype(s2v(L->top.p));
}
@ -826,15 +833,14 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
/*
** t[k] = value at the top of the stack (where 'k' is a string)
*/
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
static void auxsetstr(lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
TString *str = luaS_new(L, k);
api_checknelems(L, 1);
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
L->top.p--; /* pop value */
}
else {
} else {
setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
api_incr_top(L);
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
@ -844,7 +850,7 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
}
LUA_API void lua_setglobal (lua_State *L, const char *name) {
LUA_API void lua_setglobal(lua_State *L, const char *name) {
const TValue *G;
lua_lock(L); /* unlock done in 'auxsetstr' */
G = getGtable(L);
@ -852,7 +858,7 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) {
}
LUA_API void lua_settable (lua_State *L, int idx) {
LUA_API void lua_settable(lua_State *L, int idx) {
TValue *t;
const TValue *slot;
lua_lock(L);
@ -860,21 +866,20 @@ LUA_API void lua_settable (lua_State *L, int idx) {
t = index2value(L, idx);
if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
}
else
} else
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
L->top.p -= 2; /* pop index and value */
lua_unlock(L);
}
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
LUA_API void lua_setfield(lua_State *L, int idx, const char *k) {
lua_lock(L); /* unlock done in 'auxsetstr' */
auxsetstr(L, index2value(L, idx), k);
}
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
LUA_API void lua_seti(lua_State *L, int idx, lua_Integer n) {
TValue *t;
const TValue *slot;
lua_lock(L);
@ -882,8 +887,7 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
t = index2value(L, idx);
if (luaV_fastgeti(L, t, n, slot)) {
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
}
else {
} else {
TValue aux;
setivalue(&aux, n);
luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
@ -893,7 +897,7 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
}
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
static void aux_rawset(lua_State *L, int idx, TValue *key, int n) {
Table *t;
lua_lock(L);
api_checknelems(L, n);
@ -906,19 +910,19 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
}
LUA_API void lua_rawset (lua_State *L, int idx) {
LUA_API void lua_rawset(lua_State *L, int idx) {
aux_rawset(L, idx, s2v(L->top.p - 2), 2);
}
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
LUA_API void lua_rawsetp(lua_State *L, int idx, const void *p) {
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) {
LUA_API void lua_rawseti(lua_State *L, int idx, lua_Integer n) {
Table *t;
lua_lock(L);
api_checknelems(L, 1);
@ -930,7 +934,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
}
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
LUA_API int lua_setmetatable(lua_State *L, int objindex) {
TValue *obj;
Table *mt;
lua_lock(L);
@ -970,7 +974,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
}
LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
LUA_API int lua_setiuservalue(lua_State *L, int idx, int n) {
TValue *o;
int res;
lua_lock(L);
@ -1001,22 +1005,21 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
"results from function overflow current stack size")
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
LUA_API void lua_callk(lua_State *L, int nargs, int nresults,
lua_KContext ctx, lua_KFunction k) {
StkId func;
lua_lock(L);
api_check(L, k == NULL || !isLua(L->ci),
"cannot use continuations inside hooks");
api_checknelems(L, nargs+1);
api_checknelems(L, nargs + 1);
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
checkresults(L, nargs, nresults);
func = L->top.p - (nargs+1);
func = L->top.p - (nargs + 1);
if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
L->ci->u.c.k = k; /* save continuation */
L->ci->u.c.ctx = ctx; /* save context */
luaD_call(L, func, nresults); /* do the call */
}
else /* no continuation or no yieldable */
} else /* no continuation or no yieldable */
luaD_callnoyield(L, func, nresults); /* just do the call */
adjustresults(L, nresults);
lua_unlock(L);
@ -1033,14 +1036,14 @@ struct CallS { /* data to 'f_call' */
};
static void f_call (lua_State *L, void *ud) {
static void f_call(lua_State *L, void *ud) {
struct CallS *c = cast(struct CallS *, ud);
luaD_callnoyield(L, c->func, c->nresults);
}
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
LUA_API int lua_pcallk(lua_State *L, int nargs, int nresults, int errfunc,
lua_KContext ctx, lua_KFunction k) {
struct CallS c;
int status;
@ -1048,7 +1051,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
lua_lock(L);
api_check(L, k == NULL || !isLua(L->ci),
"cannot use continuations inside hooks");
api_checknelems(L, nargs+1);
api_checknelems(L, nargs + 1);
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
checkresults(L, nargs, nresults);
if (errfunc == 0)
@ -1058,12 +1061,11 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
func = savestack(L, o);
}
c.func = L->top.p - (nargs+1); /* function to be called */
c.func = L->top.p - (nargs + 1); /* function to be called */
if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
c.nresults = nresults; /* do a 'conventional' protected call */
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
}
else { /* prepare continuation (call is already protected by 'resume') */
} else { /* prepare continuation (call is already protected by 'resume') */
CallInfo *ci = L->ci;
ci->u.c.k = k; /* save continuation */
ci->u.c.ctx = ctx; /* save context */
@ -1084,7 +1086,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
}
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
const char *chunkname, const char *mode) {
ZIO z;
int status;
@ -1107,7 +1109,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
}
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data, int strip) {
int status;
TValue *o;
lua_lock(L);
@ -1122,7 +1124,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
}
LUA_API int lua_status (lua_State *L) {
LUA_API int lua_status(lua_State *L) {
return L->status;
}
@ -1130,7 +1132,7 @@ LUA_API int lua_status (lua_State *L) {
/*
** Garbage-collection function
*/
LUA_API int lua_gc (lua_State *L, int what, ...) {
LUA_API int lua_gc(lua_State *L, int what, ...) {
va_list argp;
int res = 0;
global_State *g = G(L);
@ -1169,8 +1171,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
if (data == 0) {
luaE_setdebt(g, 0); /* do a basic step */
luaC_step(L);
}
else { /* add 'data' to total debt */
} else { /* add 'data' to total debt */
debt = cast(l_mem, data) * 1024 + g->GCdebt;
luaE_setdebt(g, debt);
luaC_checkGC(L);
@ -1221,7 +1222,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
luaC_changemode(L, KGC_INC);
break;
}
default: res = -1; /* invalid option */
default:
res = -1; /* invalid option */
}
va_end(argp);
lua_unlock(L);
@ -1235,7 +1237,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
*/
LUA_API int lua_error (lua_State *L) {
LUA_API int lua_error(lua_State *L) {
TValue *errobj;
lua_lock(L);
errobj = s2v(L->top.p - 1);
@ -1250,7 +1252,7 @@ LUA_API int lua_error (lua_State *L) {
}
LUA_API int lua_next (lua_State *L, int idx) {
LUA_API int lua_next(lua_State *L, int idx) {
Table *t;
int more;
lua_lock(L);
@ -1259,15 +1261,14 @@ LUA_API int lua_next (lua_State *L, int idx) {
more = luaH_next(L, t, L->top.p - 1);
if (more) {
api_incr_top(L);
}
else /* no more elements */
} else /* no more elements */
L->top.p -= 1; /* remove key */
lua_unlock(L);
return more;
}
LUA_API void lua_toclose (lua_State *L, int idx) {
LUA_API void lua_toclose(lua_State *L, int idx) {
int nresults;
StkId o;
lua_lock(L);
@ -1282,7 +1283,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
}
LUA_API void lua_concat (lua_State *L, int n) {
LUA_API void lua_concat(lua_State *L, int n) {
lua_lock(L);
api_checknelems(L, n);
if (n > 0)
@ -1296,7 +1297,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
}
LUA_API void lua_len (lua_State *L, int idx) {
LUA_API void lua_len(lua_State *L, int idx) {
TValue *t;
lua_lock(L);
t = index2value(L, idx);
@ -1306,7 +1307,7 @@ LUA_API void lua_len (lua_State *L, int idx) {
}
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud) {
lua_Alloc f;
lua_lock(L);
if (ud) *ud = G(L)->ud;
@ -1316,7 +1317,7 @@ LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
}
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) {
lua_lock(L);
G(L)->ud = ud;
G(L)->frealloc = f;
@ -1324,7 +1325,7 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
}
void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
void lua_setwarnf(lua_State *L, lua_WarnFunction f, void *ud) {
lua_lock(L);
G(L)->ud_warn = ud;
G(L)->warnf = f;
@ -1332,7 +1333,7 @@ void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
}
void lua_warning (lua_State *L, const char *msg, int tocont) {
void lua_warning(lua_State *L, const char *msg, int tocont) {
lua_lock(L);
luaE_warning(L, msg, tocont);
lua_unlock(L);
@ -1340,7 +1341,7 @@ void lua_warning (lua_State *L, const char *msg, int tocont) {
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
LUA_API void *lua_newuserdatauv(lua_State *L, size_t size, int nuvalue) {
Udata *u;
lua_lock(L);
api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
@ -1354,14 +1355,14 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
static const char *aux_upvalue(TValue *fi, int n, TValue **val,
GCObject **owner) {
switch (ttypetag(fi)) {
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] */
*val = &f->upvalue[n-1];
*val = &f->upvalue[n - 1];
if (owner) *owner = obj2gco(f);
return "";
}
@ -1371,17 +1372,18 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val,
Proto *p = f->p;
if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
return NULL; /* 'n' not in [1, p->sizeupvalues] */
*val = f->upvals[n-1]->v.p;
*val = f->upvals[n - 1]->v.p;
if (owner) *owner = obj2gco(f->upvals[n - 1]);
name = p->upvalues[n-1].name;
name = p->upvalues[n - 1].name;
return (name == NULL) ? "(no name)" : getstr(name);
}
default: return NULL; /* not a closure */
default:
return NULL; /* not a closure */
}
}
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
LUA_API const char *lua_getupvalue(lua_State *L, int funcindex, int n) {
const char *name;
TValue *val = NULL; /* to avoid warnings */
lua_lock(L);
@ -1395,7 +1397,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
}
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
LUA_API const char *lua_setupvalue(lua_State *L, int funcindex, int n) {
const char *name;
TValue *val = NULL; /* to avoid warnings */
GCObject *owner = NULL; /* to avoid warnings */
@ -1414,7 +1416,7 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
}
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
static UpVal **getupvalref(lua_State *L, int fidx, int n, LClosure **pf) {
static const UpVal *const nullup = NULL;
LClosure *f;
TValue *fi = index2value(L, fidx);
@ -1424,11 +1426,11 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
if (1 <= n && n <= f->p->sizeupvalues)
return &f->upvals[n - 1]; /* get its upvalue pointer */
else
return (UpVal**)&nullup;
return (UpVal **)&nullup;
}
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
LUA_API void *lua_upvalueid(lua_State *L, int fidx, int n) {
TValue *fi = index2value(L, fidx);
switch (ttypetag(fi)) {
case LUA_VLCL: { /* lua closure */
@ -1450,7 +1452,7 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
}
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
LUA_API void lua_upvaluejoin(lua_State *L, int fidx1, int n1,
int fidx2, int n2) {
LClosure *f1;
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);

View file

@ -49,7 +49,7 @@
** Search for 'objidx' in table at index -1. ('objidx' must be an
** absolute index.) Return 1 + string at top if it found a good name.
*/
static int findfield (lua_State *L, int objidx, int level) {
static int findfield(lua_State *L, int objidx, int level) {
if (level == 0 || !lua_istable(L, -1))
return 0; /* not found */
lua_pushnil(L); /* start 'next' loop */
@ -58,8 +58,7 @@ static int findfield (lua_State *L, int objidx, int level) {
if (lua_rawequal(L, objidx, -1)) { /* found object? */
lua_pop(L, 1); /* remove value (but keep name) */
return 1;
}
else if (findfield(L, objidx, level - 1)) { /* try recursively */
} else if (findfield(L, objidx, level - 1)) { /* try recursively */
/* stack: lib_name, lib_table, field_name (top) */
lua_pushliteral(L, "."); /* place '.' between the two names */
lua_replace(L, -3); /* (in the slot occupied by table) */
@ -76,7 +75,7 @@ static int findfield (lua_State *L, int objidx, int level) {
/*
** Search for a name for a function in all loaded modules
*/
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
static int pushglobalfuncname(lua_State *L, lua_Debug *ar) {
int top = lua_gettop(L);
lua_getinfo(L, "f", ar); /* push function */
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
@ -90,20 +89,18 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
lua_copy(L, -1, top + 1); /* copy name to proper place */
lua_settop(L, top + 1); /* remove table "loaded" and name copy */
return 1;
}
else {
} else {
lua_settop(L, top); /* remove function and global table */
return 0;
}
}
static void pushfuncname (lua_State *L, lua_Debug *ar) {
static void pushfuncname(lua_State *L, lua_Debug *ar) {
if (pushglobalfuncname(L, ar)) { /* try first a global name */
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
lua_remove(L, -2); /* remove name */
}
else if (*ar->namewhat != '\0') /* is there a name from code? */
} else if (*ar->namewhat != '\0') /* is there a name from code? */
lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
else if (*ar->what == 'm') /* main? */
lua_pushliteral(L, "main chunk");
@ -114,14 +111,14 @@ static void pushfuncname (lua_State *L, lua_Debug *ar) {
}
static int lastlevel (lua_State *L) {
static int lastlevel(lua_State *L) {
lua_Debug ar;
int li = 1, le = 1;
/* find an upper bound */
while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
/* do a binary search */
while (li < le) {
int m = (li + le)/2;
int m = (li + le) / 2;
if (lua_getstack(L, m, &ar)) li = m + 1;
else le = m;
}
@ -129,7 +126,7 @@ static int lastlevel (lua_State *L) {
}
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1,
const char *msg, int level) {
luaL_Buffer b;
lua_Debug ar;
@ -147,8 +144,7 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n);
luaL_addvalue(&b); /* add warning about skip */
level += n; /* and skip to last levels */
}
else {
} else {
lua_getinfo(L1, "Slnt", &ar);
if (ar.currentline <= 0)
lua_pushfstring(L, "\n\t%s: in ", ar.short_src);
@ -173,7 +169,7 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
** =======================================================
*/
LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg) {
lua_Debug ar;
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
@ -191,7 +187,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
}
LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
LUALIB_API int luaL_typeerror(lua_State *L, int arg, const char *tname) {
const char *msg;
const char *typearg; /* name for the type of the actual argument */
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
@ -205,7 +201,7 @@ LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
}
static void tag_error (lua_State *L, int arg, int tag) {
static void tag_error(lua_State *L, int arg, int tag) {
luaL_typeerror(L, arg, lua_typename(L, tag));
}
@ -214,7 +210,7 @@ static void tag_error (lua_State *L, int arg, int tag) {
** The use of 'lua_pushfstring' ensures this function does not
** need reserved stack space when called.
*/
LUALIB_API void luaL_where (lua_State *L, int level) {
LUALIB_API void luaL_where(lua_State *L, int level) {
lua_Debug ar;
if (lua_getstack(L, level, &ar)) { /* check function at level */
lua_getinfo(L, "Sl", &ar); /* get info about it */
@ -232,7 +228,7 @@ LUALIB_API void luaL_where (lua_State *L, int level) {
** not need reserved stack space when called. (At worst, it generates
** an error with "stack overflow" instead of the given message.)
*/
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
luaL_where(L, 1);
@ -243,13 +239,12 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
}
LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname) {
int en = errno; /* calls to Lua API may change this value */
if (stat) {
lua_pushboolean(L, 1);
return 1;
}
else {
} else {
const char *msg;
luaL_pushfail(L);
msg = (en != 0) ? strerror(en) : "(no extra info)";
@ -285,7 +280,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
#endif /* } */
LUALIB_API int luaL_execresult (lua_State *L, int stat) {
LUALIB_API int luaL_execresult(lua_State *L, int stat) {
if (stat != 0 && errno != 0) /* error with an 'errno'? */
return luaL_fileresult(L, 0, NULL);
else {
@ -311,7 +306,7 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
** =======================================================
*/
LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname) {
if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */
return 0; /* leave previous value on top, but return 0 */
lua_pop(L, 1);
@ -324,13 +319,13 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
}
LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname) {
luaL_getmetatable(L, tname);
lua_setmetatable(L, -2);
}
LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
LUALIB_API void *luaL_testudata(lua_State *L, int ud, const char *tname) {
void *p = lua_touserdata(L, ud);
if (p != NULL) { /* value is a userdata? */
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
@ -345,7 +340,7 @@ LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
}
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
LUALIB_API void *luaL_checkudata(lua_State *L, int ud, const char *tname) {
void *p = luaL_testudata(L, ud, tname);
luaL_argexpected(L, p != NULL, ud, tname);
return p;
@ -360,12 +355,12 @@ LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
** =======================================================
*/
LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
LUALIB_API int luaL_checkoption(lua_State *L, int arg, const char *def,
const char *const lst[]) {
const char *name = (def) ? luaL_optstring(L, arg, def) :
luaL_checkstring(L, arg);
int i;
for (i=0; lst[i]; i++)
for (i = 0; lst[i]; i++)
if (strcmp(lst[i], name) == 0)
return i;
return luaL_argerror(L, arg,
@ -380,7 +375,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
** this extra space, Lua will generate the same 'stack overflow' error,
** but without 'msg'.)
*/
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *msg) {
if (l_unlikely(!lua_checkstack(L, space))) {
if (msg)
luaL_error(L, "stack overflow (%s)", msg);
@ -390,37 +385,36 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
}
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
LUALIB_API void luaL_checktype(lua_State *L, int arg, int t) {
if (l_unlikely(lua_type(L, arg) != t))
tag_error(L, arg, t);
}
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
LUALIB_API void luaL_checkany(lua_State *L, int arg) {
if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
luaL_argerror(L, arg, "value expected");
}
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
LUALIB_API const char *luaL_checklstring(lua_State *L, int arg, size_t *len) {
const char *s = lua_tolstring(L, arg, len);
if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
return s;
}
LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
LUALIB_API const char *luaL_optlstring(lua_State *L, int arg,
const char *def, size_t *len) {
if (lua_isnoneornil(L, arg)) {
if (len)
*len = (def ? strlen(def) : 0);
return def;
}
else return luaL_checklstring(L, arg, len);
} else return luaL_checklstring(L, arg, len);
}
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int arg) {
int isnum;
lua_Number d = lua_tonumberx(L, arg, &isnum);
if (l_unlikely(!isnum))
@ -429,12 +423,12 @@ LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
}
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int arg, lua_Number def) {
return luaL_opt(L, luaL_checknumber, arg, def);
}
static void interror (lua_State *L, int arg) {
static void interror(lua_State *L, int arg) {
if (lua_isnumber(L, arg))
luaL_argerror(L, arg, "number has no integer representation");
else
@ -442,7 +436,7 @@ static void interror (lua_State *L, int arg) {
}
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg) {
int isnum;
lua_Integer d = lua_tointegerx(L, arg, &isnum);
if (l_unlikely(!isnum)) {
@ -452,7 +446,7 @@ LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
}
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int arg,
lua_Integer def) {
return luaL_opt(L, luaL_checkinteger, arg, def);
}
@ -473,7 +467,7 @@ typedef struct UBox {
} UBox;
static void *resizebox (lua_State *L, int idx, size_t newsize) {
static void *resizebox(lua_State *L, int idx, size_t newsize) {
void *ud;
lua_Alloc allocf = lua_getallocf(L, &ud);
UBox *box = (UBox *)lua_touserdata(L, idx);
@ -488,7 +482,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
}
static int boxgc (lua_State *L) {
static int boxgc(lua_State *L) {
resizebox(L, 1, 0);
return 0;
}
@ -501,7 +495,7 @@ static const luaL_Reg boxmt[] = { /* box metamethods */
};
static void newbox (lua_State *L) {
static void newbox(lua_State *L) {
UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0);
box->box = NULL;
box->bsize = 0;
@ -532,7 +526,7 @@ static void newbox (lua_State *L) {
** bytes. (The test for "not big enough" also gets the case when the
** computation of 'newsize' overflows.)
*/
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
static size_t newbuffsize(luaL_Buffer *B, size_t sz) {
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
return luaL_error(B->L, "buffer too large");
@ -547,7 +541,7 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
** 'B'. 'boxidx' is the relative position in the stack where is the
** buffer's box or its placeholder.
*/
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
static char *prepbuffsize(luaL_Buffer *B, size_t sz, int boxidx) {
checkbufferlevel(B, boxidx);
if (B->size - B->n >= sz) /* enough space? */
return B->b + B->n;
@ -575,12 +569,12 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
/*
** returns a pointer to a free area with at least 'sz' bytes
*/
LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
LUALIB_API char *luaL_prepbuffsize(luaL_Buffer *B, size_t sz) {
return prepbuffsize(B, sz, -1);
}
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l) {
if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */
char *b = prepbuffsize(B, l, -1);
memcpy(b, s, l * sizeof(char));
@ -589,12 +583,12 @@ LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
}
LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s) {
luaL_addlstring(B, s, strlen(s));
}
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
LUALIB_API void luaL_pushresult(luaL_Buffer *B) {
lua_State *L = B->L;
checkbufferlevel(B, -1);
lua_pushlstring(L, B->b, B->n);
@ -604,7 +598,7 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
}
LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
LUALIB_API void luaL_pushresultsize(luaL_Buffer *B, size_t sz) {
luaL_addsize(B, sz);
luaL_pushresult(B);
}
@ -619,7 +613,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
** trigger an emergency GC, so we should not remove the string from the
** stack before we have the space guaranteed.)
*/
LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
LUALIB_API void luaL_addvalue(luaL_Buffer *B) {
lua_State *L = B->L;
size_t len;
const char *s = lua_tolstring(L, -1, &len);
@ -630,16 +624,16 @@ LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
}
LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B) {
B->L = L;
B->b = B->init.b;
B->n = 0;
B->size = LUAL_BUFFERSIZE;
lua_pushlightuserdata(L, (void*)B); /* push placeholder */
lua_pushlightuserdata(L, (void *)B); /* push placeholder */
}
LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
LUALIB_API char *luaL_buffinitsize(lua_State *L, luaL_Buffer *B, size_t sz) {
luaL_buffinit(L, B);
return prepbuffsize(B, sz, -1);
}
@ -661,7 +655,7 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
** t[freelist] is the index of a first free index, or zero if list is
** empty; t[t[freelist]] is the index of the second element; etc.
*/
LUALIB_API int luaL_ref (lua_State *L, int t) {
LUALIB_API int luaL_ref(lua_State *L, int t) {
int ref;
if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* remove from stack */
@ -672,8 +666,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
ref = 0; /* list is empty */
lua_pushinteger(L, 0); /* initialize as an empty list */
lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */
}
else { /* already initialized */
} else { /* already initialized */
lua_assert(lua_isinteger(L, -1));
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
}
@ -681,15 +674,14 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
if (ref != 0) { /* any free element? */
lua_rawgeti(L, t, ref); /* remove it from list */
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
}
else /* no free elements */
} else /* no free elements */
ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
lua_rawseti(L, t, ref);
return ref;
}
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
LUALIB_API void luaL_unref(lua_State *L, int t, int ref) {
if (ref >= 0) {
t = lua_absindex(L, t);
lua_rawgeti(L, t, freelist);
@ -716,14 +708,13 @@ typedef struct LoadF {
} LoadF;
static const char *getF (lua_State *L, void *ud, size_t *size) {
static const char *getF(lua_State *L, void *ud, size_t *size) {
LoadF *lf = (LoadF *)ud;
(void)L; /* not used */
if (lf->n > 0) { /* are there pre-read characters to be read? */
*size = lf->n; /* return them (chars already in buffer) */
lf->n = 0; /* no more pre-read characters */
}
else { /* read a block from file */
} else { /* read a block from file */
/* 'fread' can return > 0 *and* set the EOF flag. If next call to
'getF' called 'fread', it might still wait for user input.
The next check avoids this problem. */
@ -734,7 +725,7 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
}
static int errfile (lua_State *L, const char *what, int fnameindex) {
static int errfile(lua_State *L, const char *what, int fnameindex) {
int err = errno;
const char *filename = lua_tostring(L, fnameindex) + 1;
if (err != 0)
@ -752,7 +743,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
** not), returns the first character anyway to force an error
** (as no chunk can start with 0xEF).
*/
static int skipBOM (FILE *f) {
static int skipBOM(FILE *f) {
int c = getc(f); /* read first character */
if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */
return getc(f); /* ignore BOM and return next char */
@ -768,7 +759,7 @@ static int skipBOM (FILE *f) {
** first "valid" character of the file (after the optional BOM and
** a first-line comment).
*/
static int skipcomment (FILE *f, int *cp) {
static int skipcomment(FILE *f, int *cp) {
int c = *cp = skipBOM(f);
if (c == '#') { /* first line is a comment (Unix exec. file)? */
do { /* skip first line */
@ -776,12 +767,11 @@ static int skipcomment (FILE *f, int *cp) {
} while (c != EOF && c != '\n');
*cp = getc(f); /* next character after comment, if present */
return 1; /* there was a comment */
}
else return 0; /* no comment */
} else return 0; /* no comment */
}
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename,
const char *mode) {
LoadF lf;
int status, readstatus;
@ -790,8 +780,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
if (filename == NULL) {
lua_pushliteral(L, "=stdin");
lf.f = stdin;
}
else {
} else {
lua_pushfstring(L, "@%s", filename);
errno = 0;
lf.f = fopen(filename, "r");
@ -830,7 +819,7 @@ typedef struct LoadS {
} LoadS;
static const char *getS (lua_State *L, void *ud, size_t *size) {
static const char *getS(lua_State *L, void *ud, size_t *size) {
LoadS *ls = (LoadS *)ud;
(void)L; /* not used */
if (ls->size == 0) return NULL;
@ -840,7 +829,7 @@ static const char *getS (lua_State *L, void *ud, size_t *size) {
}
LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t size,
const char *name, const char *mode) {
LoadS ls;
ls.s = buff;
@ -849,7 +838,7 @@ LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
}
LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
LUALIB_API int luaL_loadstring(lua_State *L, const char *s) {
return luaL_loadbuffer(L, s, strlen(s), s);
}
@ -857,7 +846,7 @@ LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
LUALIB_API int luaL_getmetafield(lua_State *L, int obj, const char *event) {
if (!lua_getmetatable(L, obj)) /* no metatable? */
return LUA_TNIL;
else {
@ -873,7 +862,7 @@ LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
}
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
LUALIB_API int luaL_callmeta(lua_State *L, int obj, const char *event) {
obj = lua_absindex(L, obj);
if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */
return 0;
@ -883,7 +872,7 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
}
LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
LUALIB_API lua_Integer luaL_len(lua_State *L, int idx) {
lua_Integer l;
int isnum;
lua_len(L, idx);
@ -895,13 +884,12 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
}
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
idx = lua_absindex(L,idx);
LUALIB_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len) {
idx = lua_absindex(L, idx);
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
if (!lua_isstring(L, -1))
luaL_error(L, "'__tostring' must return a string");
}
else {
} else {
switch (lua_type(L, idx)) {
case LUA_TNUMBER: {
if (lua_isinteger(L, idx))
@ -939,7 +927,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
** function gets the 'nup' elements at the top as upvalues.
** Returns with only the table at the stack.
*/
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
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 */
if (l->func == NULL) /* placeholder? */
@ -960,7 +948,7 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
** ensure that stack[idx][fname] has a table and push that table
** into the stack
*/
LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
LUALIB_API int luaL_getsubtable(lua_State *L, int idx, const char *fname) {
if (lua_getfield(L, idx, fname) == LUA_TTABLE)
return 1; /* table already there */
else {
@ -980,7 +968,7 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
** if 'glb' is true, also registers the result in the global table.
** Leaves resulting module on the top.
*/
LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
LUALIB_API void luaL_requiref(lua_State *L, const char *modname,
lua_CFunction openf, int glb) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, -1, modname); /* LOADED[modname] */
@ -1000,7 +988,7 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
}
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
LUALIB_API void luaL_addgsub(luaL_Buffer *b, const char *s,
const char *p, const char *r) {
const char *wild;
size_t l = strlen(p);
@ -1013,7 +1001,7 @@ LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
}
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
LUALIB_API const char *luaL_gsub(lua_State *L, const char *s,
const char *p, const char *r) {
luaL_Buffer b;
luaL_buffinit(L, &b);
@ -1023,13 +1011,13 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
}
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
(void)osize; /* not used */
if (nsize == 0) {
free(ptr);
return NULL;
}
else
} else
return realloc(ptr, nsize);
}
@ -1038,7 +1026,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
** Standard panic funcion just prints an error message. The test
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
*/
static int panic (lua_State *L) {
static int panic(lua_State *L) {
const char *msg = (lua_type(L, -1) == LUA_TSTRING)
? lua_tostring(L, -1)
: "error object is not a string";
@ -1054,16 +1042,16 @@ static int panic (lua_State *L) {
** warnfon: ready to start a new message
** warnfcont: previous message is to be continued
*/
static void warnfoff (void *ud, const char *message, int tocont);
static void warnfon (void *ud, const char *message, int tocont);
static void warnfcont (void *ud, const char *message, int tocont);
static void warnfoff(void *ud, const char *message, int tocont);
static void warnfon(void *ud, const char *message, int tocont);
static void warnfcont(void *ud, const char *message, int tocont);
/*
** Check whether message is a control message. If so, execute the
** control or ignore it if unknown.
*/
static int checkcontrol (lua_State *L, const char *message, int tocont) {
static int checkcontrol(lua_State *L, const char *message, int tocont) {
if (tocont || *(message++) != '@') /* not a control message? */
return 0;
else {
@ -1076,7 +1064,7 @@ static int checkcontrol (lua_State *L, const char *message, int tocont) {
}
static void warnfoff (void *ud, const char *message, int tocont) {
static void warnfoff(void *ud, const char *message, int tocont) {
checkcontrol((lua_State *)ud, message, tocont);
}
@ -1085,7 +1073,7 @@ static void warnfoff (void *ud, const char *message, int tocont) {
** Writes the message and handle 'tocont', finishing the message
** if needed and setting the next warn function.
*/
static void warnfcont (void *ud, const char *message, int tocont) {
static void warnfcont(void *ud, const char *message, int tocont) {
lua_State *L = (lua_State *)ud;
lua_writestringerror("%s", message); /* write message */
if (tocont) /* not the last part? */
@ -1097,7 +1085,7 @@ static void warnfcont (void *ud, const char *message, int tocont) {
}
static void warnfon (void *ud, const char *message, int tocont) {
static void warnfon(void *ud, const char *message, int tocont) {
if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */
return; /* nothing else to be done */
lua_writestringerror("%s", "Lua warning: "); /* start a new warning */
@ -1105,7 +1093,7 @@ static void warnfon (void *ud, const char *message, int tocont) {
}
LUALIB_API lua_State *luaL_newstate (void) {
LUALIB_API lua_State *luaL_newstate(void) {
lua_State *L = lua_newstate(l_alloc, NULL);
if (l_likely(L)) {
lua_atpanic(L, &panic);
@ -1115,7 +1103,7 @@ LUALIB_API lua_State *luaL_newstate (void) {
}
LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
LUALIB_API void luaL_checkversion_(lua_State *L, lua_Number ver, size_t sz) {
lua_Number v = lua_version(L);
if (sz != LUAL_NUMSIZES) /* check numeric types */
luaL_error(L, "core and library have incompatible numeric types");

View file

@ -43,78 +43,78 @@ typedef struct luaL_Reg {
#define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
LUALIB_API void (luaL_checkversion_)(lua_State *L, lua_Number ver, size_t sz);
#define luaL_checkversion(L) \
luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname);
LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
LUALIB_API int (luaL_getmetafield)(lua_State *L, int obj, const char *e);
LUALIB_API int (luaL_callmeta)(lua_State *L, int obj, const char *e);
LUALIB_API const char *(luaL_tolstring)(lua_State *L, int idx, size_t *len);
LUALIB_API int (luaL_argerror)(lua_State *L, int arg, const char *extramsg);
LUALIB_API int (luaL_typeerror)(lua_State *L, int arg, const char *tname);
LUALIB_API const char *(luaL_checklstring)(lua_State *L, int arg,
size_t *l);
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
LUALIB_API const char *(luaL_optlstring)(lua_State *L, int arg,
const char *def, size_t *l);
LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg);
LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def);
LUALIB_API lua_Number(luaL_checknumber)(lua_State *L, int arg);
LUALIB_API lua_Number(luaL_optnumber)(lua_State *L, int arg, lua_Number def);
LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg);
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg,
LUALIB_API lua_Integer(luaL_checkinteger)(lua_State *L, int arg);
LUALIB_API lua_Integer(luaL_optinteger)(lua_State *L, int arg,
lua_Integer def);
LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t);
LUALIB_API void (luaL_checkany) (lua_State *L, int arg);
LUALIB_API void (luaL_checkstack)(lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_checktype)(lua_State *L, int arg, int t);
LUALIB_API void (luaL_checkany)(lua_State *L, int arg);
LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname);
LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
LUALIB_API int (luaL_newmetatable)(lua_State *L, const char *tname);
LUALIB_API void (luaL_setmetatable)(lua_State *L, const char *tname);
LUALIB_API void *(luaL_testudata)(lua_State *L, int ud, const char *tname);
LUALIB_API void *(luaL_checkudata)(lua_State *L, int ud, const char *tname);
LUALIB_API void (luaL_where) (lua_State *L, int lvl);
LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
LUALIB_API void (luaL_where)(lua_State *L, int lvl);
LUALIB_API int (luaL_error)(lua_State *L, const char *fmt, ...);
LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_checkoption)(lua_State *L, int arg, const char *def,
const char *const lst[]);
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
LUALIB_API int (luaL_fileresult)(lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult)(lua_State *L, int stat);
/* predefined references */
#define LUA_NOREF (-2)
#define LUA_REFNIL (-1)
LUALIB_API int (luaL_ref) (lua_State *L, int t);
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
LUALIB_API int (luaL_ref)(lua_State *L, int t);
LUALIB_API void (luaL_unref)(lua_State *L, int t, int ref);
LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
LUALIB_API int (luaL_loadfilex)(lua_State *L, const char *filename,
const char *mode);
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
LUALIB_API int (luaL_loadbufferx)(lua_State *L, const char *buff, size_t sz,
const char *name, const char *mode);
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
LUALIB_API int (luaL_loadstring)(lua_State *L, const char *s);
LUALIB_API lua_State *(luaL_newstate) (void);
LUALIB_API lua_State *(luaL_newstate)(void);
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
LUALIB_API lua_Integer(luaL_len)(lua_State *L, int idx);
LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
LUALIB_API void (luaL_addgsub)(luaL_Buffer *b, const char *s,
const char *p, const char *r);
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
LUALIB_API const char *(luaL_gsub)(lua_State *L, const char *s,
const char *p, const char *r);
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
LUALIB_API void (luaL_setfuncs)(lua_State *L, const luaL_Reg *l, int nup);
LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
LUALIB_API int (luaL_getsubtable)(lua_State *L, int idx, const char *fname);
LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
LUALIB_API void (luaL_traceback)(lua_State *L, lua_State *L1,
const char *msg, int level);
LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
LUALIB_API void (luaL_requiref)(lua_State *L, const char *modname,
lua_CFunction openf, int glb);
/*
@ -172,10 +172,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
#if !defined(lua_assert)
#if defined LUAI_ASSERT
#include <assert.h>
#define lua_assert(c) assert(c)
#include <assert.h>
#define lua_assert(c) assert(c)
#else
#define lua_assert(c) ((void)0)
#define lua_assert(c) ((void)0)
#endif
#endif
@ -212,14 +212,14 @@ struct luaL_Buffer {
#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);
LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz);
LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
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);
LUALIB_API void (luaL_addstring)(luaL_Buffer *B, const char *s);
LUALIB_API void (luaL_addvalue)(luaL_Buffer *B);
LUALIB_API void (luaL_pushresult)(luaL_Buffer *B);
LUALIB_API void (luaL_pushresultsize)(luaL_Buffer *B, size_t sz);
LUALIB_API char *(luaL_buffinitsize)(lua_State *L, luaL_Buffer *B, size_t sz);
#define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)

View file

@ -21,7 +21,7 @@
#include "lualib.h"
static int luaB_print (lua_State *L) {
static int luaB_print(lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
for (i = 1; i <= n; i++) { /* for each argument */
@ -42,7 +42,7 @@ static int luaB_print (lua_State *L) {
** Check first for errors; otherwise an error may interrupt
** the composition of a warning, leaving it unfinished.
*/
static int luaB_warn (lua_State *L) {
static int luaB_warn(lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
luaL_checkstring(L, 1); /* at least one argument */
@ -57,7 +57,7 @@ static int luaB_warn (lua_State *L) {
#define SPACECHARS " \f\n\r\t\v"
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
static const char *b_str2int(const char *s, int base, lua_Integer *pn) {
lua_Unsigned n = 0;
int neg = 0;
s += strspn(s, SPACECHARS); /* skip initial spaces */
@ -66,8 +66,8 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
if (!isalnum((unsigned char)*s)) /* no digit? */
return NULL;
do {
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
: (toupper((unsigned char)*s) - 'A') + 10;
int digit = (isdigit((unsigned char) * s)) ? *s - '0'
: (toupper((unsigned char) * s) - 'A') + 10;
if (digit >= base) return NULL; /* invalid numeral */
n = n * base + digit;
s++;
@ -78,13 +78,12 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
}
static int luaB_tonumber (lua_State *L) {
static int luaB_tonumber(lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion? */
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
lua_settop(L, 1); /* yes; return it */
return 1;
}
else {
} else {
size_t l;
const char *s = lua_tolstring(L, 1, &l);
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
@ -92,8 +91,7 @@ static int luaB_tonumber (lua_State *L) {
/* else not a number */
luaL_checkany(L, 1); /* (but there must be some parameter) */
}
}
else {
} else {
size_t l;
const char *s;
lua_Integer n = 0; /* to avoid warnings */
@ -111,7 +109,7 @@ static int luaB_tonumber (lua_State *L) {
}
static int luaB_error (lua_State *L) {
static int luaB_error(lua_State *L) {
int level = (int)luaL_optinteger(L, 2, 1);
lua_settop(L, 1);
if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
@ -123,7 +121,7 @@ static int luaB_error (lua_State *L) {
}
static int luaB_getmetatable (lua_State *L) {
static int luaB_getmetatable(lua_State *L) {
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L);
@ -134,7 +132,7 @@ static int luaB_getmetatable (lua_State *L) {
}
static int luaB_setmetatable (lua_State *L) {
static int luaB_setmetatable(lua_State *L) {
int t = lua_type(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
@ -146,7 +144,7 @@ static int luaB_setmetatable (lua_State *L) {
}
static int luaB_rawequal (lua_State *L) {
static int luaB_rawequal(lua_State *L) {
luaL_checkany(L, 1);
luaL_checkany(L, 2);
lua_pushboolean(L, lua_rawequal(L, 1, 2));
@ -154,7 +152,7 @@ static int luaB_rawequal (lua_State *L) {
}
static int luaB_rawlen (lua_State *L) {
static int luaB_rawlen(lua_State *L) {
int t = lua_type(L, 1);
luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
"table or string");
@ -163,7 +161,7 @@ static int luaB_rawlen (lua_State *L) {
}
static int luaB_rawget (lua_State *L) {
static int luaB_rawget(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_settop(L, 2);
@ -171,7 +169,7 @@ static int luaB_rawget (lua_State *L) {
return 1;
}
static int luaB_rawset (lua_State *L) {
static int luaB_rawset(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
@ -181,7 +179,7 @@ static int luaB_rawset (lua_State *L) {
}
static int pushmode (lua_State *L, int oldmode) {
static int pushmode(lua_State *L, int oldmode) {
if (oldmode == -1)
luaL_pushfail(L); /* invalid call to 'lua_gc' */
else
@ -196,20 +194,22 @@ static int pushmode (lua_State *L, int oldmode) {
*/
#define checkvalres(res) { if (res == -1) break; }
static int luaB_collectgarbage (lua_State *L) {
static int luaB_collectgarbage(lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "setpause", "setstepmul",
"isrunning", "generational", "incremental", NULL};
"isrunning", "generational", "incremental", NULL
};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC
};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
switch (o) {
case LUA_GCCOUNT: {
int k = lua_gc(L, o);
int b = lua_gc(L, LUA_GCCOUNTB);
checkvalres(k);
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b / 1024));
return 1;
}
case LUA_GCSTEP: {
@ -256,7 +256,7 @@ static int luaB_collectgarbage (lua_State *L) {
}
static int luaB_type (lua_State *L) {
static int luaB_type(lua_State *L) {
int t = lua_type(L, 1);
luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
lua_pushstring(L, lua_typename(L, t));
@ -264,7 +264,7 @@ static int luaB_type (lua_State *L) {
}
static int luaB_next (lua_State *L) {
static int luaB_next(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
@ -276,19 +276,20 @@ static int luaB_next (lua_State *L) {
}
static int pairscont (lua_State *L, int status, lua_KContext k) {
(void)L; (void)status; (void)k; /* unused */
static int pairscont(lua_State *L, int status, lua_KContext k) {
(void)L;
(void)status;
(void)k; /* unused */
return 3;
}
static int luaB_pairs (lua_State *L) {
static int luaB_pairs(lua_State *L) {
luaL_checkany(L, 1);
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
lua_pushcfunction(L, luaB_next); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
}
else {
} else {
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
}
@ -299,7 +300,7 @@ static int luaB_pairs (lua_State *L) {
/*
** Traversal function for 'ipairs'
*/
static int ipairsaux (lua_State *L) {
static int ipairsaux(lua_State *L) {
lua_Integer i = luaL_checkinteger(L, 2);
i = luaL_intop(+, i, 1);
lua_pushinteger(L, i);
@ -311,7 +312,7 @@ static int ipairsaux (lua_State *L) {
** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
** (The given "table" may not be a table.)
*/
static int luaB_ipairs (lua_State *L) {
static int luaB_ipairs(lua_State *L) {
luaL_checkany(L, 1);
lua_pushcfunction(L, ipairsaux); /* iteration function */
lua_pushvalue(L, 1); /* state */
@ -320,7 +321,7 @@ static int luaB_ipairs (lua_State *L) {
}
static int load_aux (lua_State *L, int status, int envidx) {
static int load_aux(lua_State *L, int status, int envidx) {
if (l_likely(status == LUA_OK)) {
if (envidx != 0) { /* 'env' parameter? */
lua_pushvalue(L, envidx); /* environment for loaded function */
@ -328,8 +329,7 @@ static int load_aux (lua_State *L, int status, int envidx) {
lua_pop(L, 1); /* remove 'env' if not used by previous call */
}
return 1;
}
else { /* error (message is on top of the stack) */
} else { /* error (message is on top of the stack) */
luaL_pushfail(L);
lua_insert(L, -2); /* put before error message */
return 2; /* return fail plus error message */
@ -337,7 +337,7 @@ static int load_aux (lua_State *L, int status, int envidx) {
}
static int luaB_loadfile (lua_State *L) {
static int luaB_loadfile(lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = luaL_optstring(L, 2, NULL);
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
@ -367,7 +367,7 @@ static int luaB_loadfile (lua_State *L) {
** stack top. Instead, it keeps its resulting string in a
** reserved slot inside the stack.
*/
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
static const char *generic_reader(lua_State *L, void *ud, size_t *size) {
(void)(ud); /* not used */
luaL_checkstack(L, 2, "too many nested functions");
lua_pushvalue(L, 1); /* get function */
@ -376,15 +376,14 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
lua_pop(L, 1); /* pop result */
*size = 0;
return NULL;
}
else if (l_unlikely(!lua_isstring(L, -1)))
} else if (l_unlikely(!lua_isstring(L, -1)))
luaL_error(L, "reader function must return a string");
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
return lua_tolstring(L, RESERVEDSLOT, size);
}
static int luaB_load (lua_State *L) {
static int luaB_load(lua_State *L) {
int status;
size_t l;
const char *s = lua_tolstring(L, 1, &l);
@ -393,8 +392,7 @@ static int luaB_load (lua_State *L) {
if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s);
status = luaL_loadbufferx(L, s, l, chunkname, mode);
}
else { /* loading from a reader function */
} else { /* loading from a reader function */
const char *chunkname = luaL_optstring(L, 2, "=(load)");
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
@ -406,13 +404,14 @@ static int luaB_load (lua_State *L) {
/* }====================================================== */
static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
(void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
static int dofilecont(lua_State *L, int d1, lua_KContext d2) {
(void)d1;
(void)d2; /* only to match 'lua_Kfunction' prototype */
return lua_gettop(L) - 1;
}
static int luaB_dofile (lua_State *L) {
static int luaB_dofile(lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1);
if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
@ -422,7 +421,7 @@ static int luaB_dofile (lua_State *L) {
}
static int luaB_assert (lua_State *L) {
static int luaB_assert(lua_State *L) {
if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
return lua_gettop(L); /* return all arguments */
else { /* error */
@ -435,13 +434,12 @@ static int luaB_assert (lua_State *L) {
}
static int luaB_select (lua_State *L) {
static int luaB_select(lua_State *L) {
int n = lua_gettop(L);
if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
lua_pushinteger(L, n-1);
lua_pushinteger(L, n - 1);
return 1;
}
else {
} else {
lua_Integer i = luaL_checkinteger(L, 1);
if (i < 0) i = n + i;
else if (i > n) i = n;
@ -458,18 +456,17 @@ static int luaB_select (lua_State *L) {
** 'extra' values (where 'extra' is exactly the number of items to be
** ignored).
*/
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
static int finishpcall(lua_State *L, int status, lua_KContext extra) {
if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */
lua_pushboolean(L, 0); /* first result (false) */
lua_pushvalue(L, -2); /* error message */
return 2; /* return false, msg */
}
else
} else
return lua_gettop(L) - (int)extra; /* return all results */
}
static int luaB_pcall (lua_State *L) {
static int luaB_pcall(lua_State *L) {
int status;
luaL_checkany(L, 1);
lua_pushboolean(L, 1); /* first result if no errors */
@ -484,7 +481,7 @@ static int luaB_pcall (lua_State *L) {
** stack will have <f, err, true, f, [args...]>; so, the function passes
** 2 to 'finishpcall' to skip the 2 first values when returning results.
*/
static int luaB_xpcall (lua_State *L) {
static int luaB_xpcall(lua_State *L) {
int status;
int n = lua_gettop(L);
luaL_checktype(L, 2, LUA_TFUNCTION); /* check error function */
@ -496,7 +493,7 @@ static int luaB_xpcall (lua_State *L) {
}
static int luaB_tostring (lua_State *L) {
static int luaB_tostring(lua_State *L) {
luaL_checkany(L, 1);
luaL_tolstring(L, 1, NULL);
return 1;
@ -534,7 +531,7 @@ static const luaL_Reg base_funcs[] = {
};
LUAMOD_API int luaopen_base (lua_State *L) {
LUAMOD_API int luaopen_base(lua_State *L) {
/* open lib into global table */
lua_pushglobaltable(L);
luaL_setfuncs(L, base_funcs, 0);

File diff suppressed because it is too large Load diff

View file

@ -59,43 +59,43 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
LUAI_FUNC int luaK_code(FuncState *fs, Instruction i);
LUAI_FUNC int luaK_codeABx(FuncState *fs, OpCode o, int A, unsigned int Bx);
LUAI_FUNC int luaK_codeABCk(FuncState *fs, OpCode o, int A,
int B, int C, int k);
LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_jump (FuncState *fs);
LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
LUAI_FUNC int luaK_getlabel (FuncState *fs);
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
LUAI_FUNC int luaK_exp2const(FuncState *fs, const expdesc *e, TValue *v);
LUAI_FUNC void luaK_fixline(FuncState *fs, int line);
LUAI_FUNC void luaK_nil(FuncState *fs, int from, int n);
LUAI_FUNC void luaK_reserveregs(FuncState *fs, int n);
LUAI_FUNC void luaK_checkstack(FuncState *fs, int n);
LUAI_FUNC void luaK_int(FuncState *fs, int reg, lua_Integer n);
LUAI_FUNC void luaK_dischargevars(FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_exp2anyreg(FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2anyregup(FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2nextreg(FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2val(FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_self(FuncState *fs, expdesc *e, expdesc *key);
LUAI_FUNC void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k);
LUAI_FUNC void luaK_goiftrue(FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_goiffalse(FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_storevar(FuncState *fs, expdesc *var, expdesc *e);
LUAI_FUNC void luaK_setreturns(FuncState *fs, expdesc *e, int nresults);
LUAI_FUNC void luaK_setoneret(FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_jump(FuncState *fs);
LUAI_FUNC void luaK_ret(FuncState *fs, int first, int nret);
LUAI_FUNC void luaK_patchlist(FuncState *fs, int list, int target);
LUAI_FUNC void luaK_patchtohere(FuncState *fs, int list);
LUAI_FUNC void luaK_concat(FuncState *fs, int *l1, int l2);
LUAI_FUNC int luaK_getlabel(FuncState *fs);
LUAI_FUNC void luaK_prefix(FuncState *fs, UnOpr op, expdesc *v, int line);
LUAI_FUNC void luaK_infix(FuncState *fs, BinOpr op, expdesc *v);
LUAI_FUNC void luaK_posfix(FuncState *fs, BinOpr op, expdesc *v1,
expdesc *v2, int line);
LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc,
LUAI_FUNC void luaK_settablesize(FuncState *fs, int pc,
int ra, int asize, int hsize);
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
LUAI_FUNC void luaK_finish (FuncState *fs);
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg);
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);
#endif

View file

@ -18,7 +18,7 @@
#include "lualib.h"
static lua_State *getco (lua_State *L) {
static lua_State *getco(lua_State *L) {
lua_State *co = lua_tothread(L, 1);
luaL_argexpected(L, co, 1, "thread");
return co;
@ -29,7 +29,7 @@ static lua_State *getco (lua_State *L) {
** Resumes a coroutine. Returns the number of results for non-error
** cases or -1 for errors.
*/
static int auxresume (lua_State *L, lua_State *co, int narg) {
static int auxresume(lua_State *L, lua_State *co, int narg) {
int status, nres;
if (l_unlikely(!lua_checkstack(co, narg))) {
lua_pushliteral(L, "too many arguments to resume");
@ -45,15 +45,14 @@ static int auxresume (lua_State *L, lua_State *co, int narg) {
}
lua_xmove(co, L, nres); /* move yielded values */
return nres;
}
else {
} else {
lua_xmove(co, L, 1); /* move error message */
return -1; /* error flag */
}
}
static int luaB_coresume (lua_State *L) {
static int luaB_coresume(lua_State *L) {
lua_State *co = getco(L);
int r;
r = auxresume(L, co, lua_gettop(L) - 1);
@ -61,8 +60,7 @@ static int luaB_coresume (lua_State *L) {
lua_pushboolean(L, 0);
lua_insert(L, -2);
return 2; /* return false + error message */
}
else {
} else {
lua_pushboolean(L, 1);
lua_insert(L, -(r + 1));
return r + 1; /* return true + 'resume' returns */
@ -70,7 +68,7 @@ static int luaB_coresume (lua_State *L) {
}
static int luaB_auxwrap (lua_State *L) {
static int luaB_auxwrap(lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L));
if (l_unlikely(r < 0)) { /* error? */
@ -92,7 +90,7 @@ static int luaB_auxwrap (lua_State *L) {
}
static int luaB_cocreate (lua_State *L) {
static int luaB_cocreate(lua_State *L) {
lua_State *NL;
luaL_checktype(L, 1, LUA_TFUNCTION);
NL = lua_newthread(L);
@ -102,14 +100,14 @@ static int luaB_cocreate (lua_State *L) {
}
static int luaB_cowrap (lua_State *L) {
static int luaB_cowrap(lua_State *L) {
luaB_cocreate(L);
lua_pushcclosure(L, luaB_auxwrap, 1);
return 1;
}
static int luaB_yield (lua_State *L) {
static int luaB_yield(lua_State *L) {
return lua_yield(L, lua_gettop(L));
}
@ -121,10 +119,10 @@ static int luaB_yield (lua_State *L) {
static const char *const statname[] =
{"running", "dead", "suspended", "normal"};
{"running", "dead", "suspended", "normal"};
static int auxstatus (lua_State *L, lua_State *co) {
static int auxstatus(lua_State *L, lua_State *co) {
if (L == co) return COS_RUN;
else {
switch (lua_status(co)) {
@ -146,38 +144,38 @@ static int auxstatus (lua_State *L, lua_State *co) {
}
static int luaB_costatus (lua_State *L) {
static int luaB_costatus(lua_State *L) {
lua_State *co = getco(L);
lua_pushstring(L, statname[auxstatus(L, co)]);
return 1;
}
static int luaB_yieldable (lua_State *L) {
static int luaB_yieldable(lua_State *L) {
lua_State *co = lua_isnone(L, 1) ? L : getco(L);
lua_pushboolean(L, lua_isyieldable(co));
return 1;
}
static int luaB_corunning (lua_State *L) {
static int luaB_corunning(lua_State *L) {
int ismain = lua_pushthread(L);
lua_pushboolean(L, ismain);
return 2;
}
static int luaB_close (lua_State *L) {
static int luaB_close(lua_State *L) {
lua_State *co = getco(L);
int status = auxstatus(L, co);
switch (status) {
case COS_DEAD: case COS_YIELD: {
case COS_DEAD:
case COS_YIELD: {
status = lua_closethread(co, L);
if (status == LUA_OK) {
lua_pushboolean(L, 1);
return 1;
}
else {
} else {
lua_pushboolean(L, 0);
lua_xmove(co, L, 1); /* move error message */
return 2;
@ -203,7 +201,7 @@ static const luaL_Reg co_funcs[] = {
LUAMOD_API int luaopen_coroutine (lua_State *L) {
LUAMOD_API int luaopen_coroutine(lua_State *L) {
luaL_newlib(L, co_funcs);
return 1;
}

View file

@ -32,19 +32,19 @@ static const char *const HOOKKEY = "_HOOKKEY";
** guarantees about its stack space; any push in L1 must be
** checked.
*/
static void checkstack (lua_State *L, lua_State *L1, int n) {
static void checkstack(lua_State *L, lua_State *L1, int n) {
if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
luaL_error(L, "stack overflow");
}
static int db_getregistry (lua_State *L) {
static int db_getregistry(lua_State *L) {
lua_pushvalue(L, LUA_REGISTRYINDEX);
return 1;
}
static int db_getmetatable (lua_State *L) {
static int db_getmetatable(lua_State *L) {
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L); /* no metatable */
@ -53,7 +53,7 @@ static int db_getmetatable (lua_State *L) {
}
static int db_setmetatable (lua_State *L) {
static int db_setmetatable(lua_State *L) {
int t = lua_type(L, 2);
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
lua_settop(L, 2);
@ -62,7 +62,7 @@ static int db_setmetatable (lua_State *L) {
}
static int db_getuservalue (lua_State *L) {
static int db_getuservalue(lua_State *L) {
int n = (int)luaL_optinteger(L, 2, 1);
if (lua_type(L, 1) != LUA_TUSERDATA)
luaL_pushfail(L);
@ -74,7 +74,7 @@ static int db_getuservalue (lua_State *L) {
}
static int db_setuservalue (lua_State *L) {
static int db_setuservalue(lua_State *L) {
int n = (int)luaL_optinteger(L, 3, 1);
luaL_checktype(L, 1, LUA_TUSERDATA);
luaL_checkany(L, 2);
@ -91,12 +91,11 @@ static int db_setuservalue (lua_State *L) {
** 1 if this argument is present (so that functions can skip it to
** access their other arguments)
*/
static lua_State *getthread (lua_State *L, int *arg) {
static lua_State *getthread(lua_State *L, int *arg) {
if (lua_isthread(L, 1)) {
*arg = 1;
return lua_tothread(L, 1);
}
else {
} else {
*arg = 0;
return L; /* function will operate over current thread */
}
@ -108,17 +107,17 @@ static lua_State *getthread (lua_State *L, int *arg) {
** from 'lua_getinfo' into result table. Key is always a string;
** value can be a string, an int, or a boolean.
*/
static void settabss (lua_State *L, const char *k, const char *v) {
static void settabss(lua_State *L, const char *k, const char *v) {
lua_pushstring(L, v);
lua_setfield(L, -2, k);
}
static void settabsi (lua_State *L, const char *k, int v) {
static void settabsi(lua_State *L, const char *k, int v) {
lua_pushinteger(L, v);
lua_setfield(L, -2, k);
}
static void settabsb (lua_State *L, const char *k, int v) {
static void settabsb(lua_State *L, const char *k, int v) {
lua_pushboolean(L, v);
lua_setfield(L, -2, k);
}
@ -131,7 +130,7 @@ static void settabsb (lua_State *L, const char *k, int v) {
** 'lua_getinfo' on top of the result table so that it can call
** 'lua_setfield'.
*/
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) {
if (L == L1)
lua_rotate(L, -2, 1); /* exchange object and table */
else
@ -146,26 +145,25 @@ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
** two optional outputs (function and line table) from function
** 'lua_getinfo'.
*/
static int db_getinfo (lua_State *L) {
static int db_getinfo(lua_State *L) {
lua_Debug ar;
int arg;
lua_State *L1 = getthread(L, &arg);
const char *options = luaL_optstring(L, arg+2, "flnSrtu");
const char *options = luaL_optstring(L, arg + 2, "flnSrtu");
checkstack(L, L1, 3);
luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
lua_xmove(L, L1, 1);
}
else { /* stack level */
} else { /* stack level */
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
luaL_pushfail(L); /* level out of range */
return 1;
}
}
if (!lua_getinfo(L1, options, &ar))
return luaL_argerror(L, arg+2, "invalid option");
return luaL_argerror(L, arg + 2, "invalid option");
lua_newtable(L); /* table to collect results */
if (strchr(options, 'S')) {
lua_pushlstring(L, ar.source, ar.srclen);
@ -200,7 +198,7 @@ static int db_getinfo (lua_State *L) {
}
static int db_getlocal (lua_State *L) {
static int db_getlocal(lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
@ -208,13 +206,12 @@ static int db_getlocal (lua_State *L) {
lua_pushvalue(L, arg + 1); /* push function */
lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
return 1; /* return only name (there is no value) */
}
else { /* stack-level argument */
} else { /* stack-level argument */
lua_Debug ar;
const char *name;
int level = (int)luaL_checkinteger(L, arg + 1);
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
return luaL_argerror(L, arg+1, "level out of range");
return luaL_argerror(L, arg + 1, "level out of range");
checkstack(L, L1, 1);
name = lua_getlocal(L1, &ar, nvar);
if (name) {
@ -222,8 +219,7 @@ static int db_getlocal (lua_State *L) {
lua_pushstring(L, name); /* push name */
lua_rotate(L, -2, 1); /* re-order */
return 2;
}
else {
} else {
luaL_pushfail(L); /* no name (nor value) */
return 1;
}
@ -231,7 +227,7 @@ static int db_getlocal (lua_State *L) {
}
static int db_setlocal (lua_State *L) {
static int db_setlocal(lua_State *L) {
int arg;
const char *name;
lua_State *L1 = getthread(L, &arg);
@ -239,9 +235,9 @@ static int db_setlocal (lua_State *L) {
int level = (int)luaL_checkinteger(L, arg + 1);
int nvar = (int)luaL_checkinteger(L, arg + 2);
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
return luaL_argerror(L, arg+1, "level out of range");
luaL_checkany(L, arg+3);
lua_settop(L, arg+3);
return luaL_argerror(L, arg + 1, "level out of range");
luaL_checkany(L, arg + 3);
lua_settop(L, arg + 3);
checkstack(L, L1, 1);
lua_xmove(L, L1, 1);
name = lua_setlocal(L1, &ar, nvar);
@ -255,24 +251,24 @@ static int db_setlocal (lua_State *L) {
/*
** get (if 'get' is true) or set an upvalue from a closure
*/
static int auxupvalue (lua_State *L, int get) {
static int auxupvalue(lua_State *L, int get) {
const char *name;
int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
if (name == NULL) return 0;
lua_pushstring(L, name);
lua_insert(L, -(get+1)); /* no-op if get is false */
lua_insert(L, -(get + 1)); /* no-op if get is false */
return get + 1;
}
static int db_getupvalue (lua_State *L) {
static int db_getupvalue(lua_State *L) {
return auxupvalue(L, 1);
}
static int db_setupvalue (lua_State *L) {
static int db_setupvalue(lua_State *L) {
luaL_checkany(L, 3);
return auxupvalue(L, 0);
}
@ -282,7 +278,7 @@ static int db_setupvalue (lua_State *L) {
** Check whether a given upvalue from a given closure exists and
** returns its index
*/
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
static void *checkupval(lua_State *L, int argf, int argnup, int *pnup) {
void *id;
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
@ -295,7 +291,7 @@ static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
}
static int db_upvalueid (lua_State *L) {
static int db_upvalueid(lua_State *L) {
void *id = checkupval(L, 1, 2, NULL);
if (id != NULL)
lua_pushlightuserdata(L, id);
@ -305,7 +301,7 @@ static int db_upvalueid (lua_State *L) {
}
static int db_upvaluejoin (lua_State *L) {
static int db_upvaluejoin(lua_State *L) {
int n1, n2;
checkupval(L, 1, 2, &n1);
checkupval(L, 3, 4, &n2);
@ -320,7 +316,7 @@ static int db_upvaluejoin (lua_State *L) {
** Call hook function registered at hook table for the current
** thread (if there is one)
*/
static void hookf (lua_State *L, lua_Debug *ar) {
static void hookf(lua_State *L, lua_Debug *ar) {
static const char *const hooknames[] =
{"call", "return", "line", "count", "tail call"};
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
@ -339,7 +335,7 @@ static void hookf (lua_State *L, lua_Debug *ar) {
/*
** Convert a string mask (for 'sethook') into a bit mask
*/
static int makemask (const char *smask, int count) {
static int makemask(const char *smask, int count) {
int mask = 0;
if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
if (strchr(smask, 'r')) mask |= LUA_MASKRET;
@ -352,7 +348,7 @@ static int makemask (const char *smask, int count) {
/*
** Convert a bit mask (for 'gethook') into a string mask
*/
static char *unmakemask (int mask, char *smask) {
static char *unmakemask(int mask, char *smask) {
int i = 0;
if (mask & LUA_MASKCALL) smask[i++] = 'c';
if (mask & LUA_MASKRET) smask[i++] = 'r';
@ -362,19 +358,21 @@ static char *unmakemask (int mask, char *smask) {
}
static int db_sethook (lua_State *L) {
static int db_sethook(lua_State *L) {
int arg, mask, count;
lua_Hook func;
lua_State *L1 = getthread(L, &arg);
if (lua_isnoneornil(L, arg+1)) { /* no hook? */
lua_settop(L, arg+1);
func = NULL; mask = 0; count = 0; /* turn off hooks */
}
else {
const char *smask = luaL_checkstring(L, arg+2);
luaL_checktype(L, arg+1, LUA_TFUNCTION);
if (lua_isnoneornil(L, arg + 1)) { /* no hook? */
lua_settop(L, arg + 1);
func = NULL;
mask = 0;
count = 0; /* turn off hooks */
} else {
const char *smask = luaL_checkstring(L, arg + 2);
luaL_checktype(L, arg + 1, LUA_TFUNCTION);
count = (int)luaL_optinteger(L, arg + 3, 0);
func = hookf; mask = makemask(smask, count);
func = hookf;
mask = makemask(smask, count);
}
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
/* table just created; initialize it */
@ -384,7 +382,8 @@ static int db_sethook (lua_State *L) {
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
}
checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
lua_pushthread(L1);
lua_xmove(L1, L, 1); /* key (thread) */
lua_pushvalue(L, arg + 1); /* value (hook function) */
lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */
lua_sethook(L1, func, mask, count);
@ -392,7 +391,7 @@ static int db_sethook (lua_State *L) {
}
static int db_gethook (lua_State *L) {
static int db_gethook(lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
char buff[5];
@ -401,13 +400,13 @@ static int db_gethook (lua_State *L) {
if (hook == NULL) { /* no hook? */
luaL_pushfail(L);
return 1;
}
else if (hook != hookf) /* external hook? */
} else if (hook != hookf) /* external hook? */
lua_pushliteral(L, "external hook");
else { /* hook table must exist */
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
checkstack(L, L1, 1);
lua_pushthread(L1); lua_xmove(L1, L, 1);
lua_pushthread(L1);
lua_xmove(L1, L, 1);
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
lua_remove(L, -2); /* remove hook table */
}
@ -417,7 +416,7 @@ static int db_gethook (lua_State *L) {
}
static int db_debug (lua_State *L) {
static int db_debug(lua_State *L) {
for (;;) {
char buffer[250];
lua_writestringerror("%s", "lua_debug> ");
@ -432,7 +431,7 @@ static int db_debug (lua_State *L) {
}
static int db_traceback (lua_State *L) {
static int db_traceback(lua_State *L) {
int arg;
lua_State *L1 = getthread(L, &arg);
const char *msg = lua_tostring(L, arg + 1);
@ -446,7 +445,7 @@ static int db_traceback (lua_State *L) {
}
static int db_setcstacklimit (lua_State *L) {
static int db_setcstacklimit(lua_State *L) {
int limit = (int)luaL_checkinteger(L, 1);
int res = lua_setcstacklimit(L, limit);
lua_pushinteger(L, res);
@ -476,7 +475,7 @@ static const luaL_Reg dblib[] = {
};
LUAMOD_API int luaopen_debug (lua_State *L) {
LUAMOD_API int luaopen_debug(lua_State *L) {
luaL_newlib(L, dblib);
return 1;
}

View file

@ -34,11 +34,11 @@
#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL)
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
static const char *funcnamefromcall(lua_State *L, CallInfo *ci,
const char **name);
static int currentpc (CallInfo *ci) {
static int currentpc(CallInfo *ci) {
lua_assert(isLua(ci));
return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
}
@ -57,12 +57,11 @@ static int currentpc (CallInfo *ci) {
** value for MAXIWTHABS or smaller. (Previous releases use a little
** smaller value.)
*/
static int getbaseline (const Proto *f, int pc, int *basepc) {
static int getbaseline(const Proto *f, int pc, int *basepc) {
if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) {
*basepc = -1; /* start from the beginning */
return f->linedefined;
}
else {
} else {
int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
/* estimate must be a lower bound of the correct base */
lua_assert(i < 0 ||
@ -80,7 +79,7 @@ static int getbaseline (const Proto *f, int pc, int *basepc) {
** first gets a base line and from there does the increments until
** the desired instruction.
*/
int luaG_getfuncline (const Proto *f, int pc) {
int luaG_getfuncline(const Proto *f, int pc) {
if (f->lineinfo == NULL) /* no debug information? */
return -1;
else {
@ -95,7 +94,7 @@ int luaG_getfuncline (const Proto *f, int pc) {
}
static int getcurrentline (CallInfo *ci) {
static int getcurrentline(CallInfo *ci) {
return luaG_getfuncline(ci_func(ci)->p, currentpc(ci));
}
@ -111,7 +110,7 @@ static int getcurrentline (CallInfo *ci) {
** temporarily broken while inserting a new element. We simply assume it
** has no good reasons to do that.)
*/
static void settraps (CallInfo *ci) {
static void settraps(CallInfo *ci) {
for (; ci != NULL; ci = ci->previous)
if (isLua(ci))
ci->u.l.trap = 1;
@ -128,7 +127,7 @@ static void settraps (CallInfo *ci) {
** for all platforms where it runs). Moreover, 'hook' is always checked
** before being called (see 'luaD_hook').
*/
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
LUA_API void lua_sethook(lua_State *L, lua_Hook func, int mask, int count) {
if (func == NULL || mask == 0) { /* turn off hooks? */
mask = 0;
func = NULL;
@ -142,22 +141,22 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
}
LUA_API lua_Hook lua_gethook (lua_State *L) {
LUA_API lua_Hook lua_gethook(lua_State *L) {
return L->hook;
}
LUA_API int lua_gethookmask (lua_State *L) {
LUA_API int lua_gethookmask(lua_State *L) {
return L->hookmask;
}
LUA_API int lua_gethookcount (lua_State *L) {
LUA_API int lua_gethookcount(lua_State *L) {
return L->basehookcount;
}
LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) {
int status;
CallInfo *ci;
if (level < 0) return 0; /* invalid (negative) level */
@ -167,21 +166,20 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
if (level == 0 && ci != &L->base_ci) { /* level found? */
status = 1;
ar->i_ci = ci;
}
else status = 0; /* no such level */
} else status = 0; /* no such level */
lua_unlock(L);
return status;
}
static const char *upvalname (const Proto *p, int uv) {
static const char *upvalname(const Proto *p, int uv) {
TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
if (s == NULL) return "?";
else return getstr(s);
}
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
static const char *findvararg(CallInfo *ci, int n, StkId *pos) {
if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
int nextra = ci->u.l.nextraargs;
if (n >= -nextra) { /* 'n' is negative */
@ -193,7 +191,7 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
}
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
const char *luaG_findlocal(lua_State *L, CallInfo *ci, int n, StkId *pos) {
StkId base = ci->func.p + 1;
const char *name = NULL;
if (isLua(ci)) {
@ -207,8 +205,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
/* generic name for any valid slot */
name = isLua(ci) ? "(temporary)" : "(C temporary)";
}
else
} else
return NULL; /* no name */
}
if (pos)
@ -217,7 +214,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
}
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) {
const char *name;
lua_lock(L);
if (ar == NULL) { /* information about non-active function? */
@ -225,8 +222,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
name = NULL;
else /* consider live variables at function start (parameters) */
name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0);
}
else { /* active function; get information through 'ar' */
} else { /* active function; get information through 'ar' */
StkId pos = NULL; /* to avoid warnings */
name = luaG_findlocal(L, ar->i_ci, n, &pos);
if (name) {
@ -239,7 +235,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
}
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) {
StkId pos = NULL; /* to avoid warnings */
const char *name;
lua_lock(L);
@ -253,21 +249,19 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
}
static void funcinfo (lua_Debug *ar, Closure *cl) {
static void funcinfo(lua_Debug *ar, Closure *cl) {
if (!LuaClosure(cl)) {
ar->source = "=[C]";
ar->srclen = LL("=[C]");
ar->linedefined = -1;
ar->lastlinedefined = -1;
ar->what = "C";
}
else {
} else {
const Proto *p = cl->l.p;
if (p->source) {
ar->source = getstr(p->source);
ar->srclen = tsslen(p->source);
}
else {
} else {
ar->source = "=?";
ar->srclen = LL("=?");
}
@ -279,7 +273,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) {
}
static int nextline (const Proto *p, int currentline, int pc) {
static int nextline(const Proto *p, int currentline, int pc) {
if (p->lineinfo[pc] != ABSLINEINFO)
return currentline + p->lineinfo[pc];
else
@ -287,12 +281,11 @@ static int nextline (const Proto *p, int currentline, int pc) {
}
static void collectvalidlines (lua_State *L, Closure *f) {
static void collectvalidlines(lua_State *L, Closure *f) {
if (!LuaClosure(f)) {
setnilvalue(s2v(L->top.p));
api_incr_top(L);
}
else {
} else {
const Proto *p = f->l.p;
int currentline = p->linedefined;
Table *t = luaH_new(L); /* new table to store active lines */
@ -318,7 +311,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
}
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
static const char *getfuncname(lua_State *L, CallInfo *ci, const char **name) {
/* calling function is a known function? */
if (ci != NULL && !(ci->callstatus & CIST_TAIL))
return funcnamefromcall(L, ci->previous, name);
@ -326,7 +319,7 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
}
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
static int auxgetinfo(lua_State *L, const char *what, lua_Debug *ar,
Closure *f, CallInfo *ci) {
int status = 1;
for (; *what; what++) {
@ -344,8 +337,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
if (!LuaClosure(f)) {
ar->isvararg = 1;
ar->nparams = 0;
}
else {
} else {
ar->isvararg = f->l.p->is_vararg;
ar->nparams = f->l.p->numparams;
}
@ -375,14 +367,15 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
case 'L':
case 'f': /* handled by lua_getinfo */
break;
default: status = 0; /* invalid option */
default:
status = 0; /* invalid option */
}
}
return status;
}
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) {
int status;
Closure *cl;
CallInfo *ci;
@ -394,8 +387,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
api_check(L, ttisfunction(func), "function expected");
what++; /* skip the '>' */
L->top.p--; /* pop function */
}
else {
} else {
ci = ar->i_ci;
func = s2v(ci->func.p);
lua_assert(ttisfunction(func));
@ -420,7 +412,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
*/
static int filterpc (int pc, int jmptarget) {
static int filterpc(int pc, int jmptarget) {
if (pc < jmptarget) /* is code conditional (inside a jump)? */
return -1; /* cannot know who sets that register */
else return pc; /* current position sets that register */
@ -430,7 +422,7 @@ static int filterpc (int pc, int jmptarget) {
/*
** Try to find last instruction before 'lastpc' that modified register 'reg'.
*/
static int findsetreg (const Proto *p, int lastpc, int reg) {
static int findsetreg(const Proto *p, int lastpc, int reg) {
int pc;
int setreg = -1; /* keep last instruction that changed 'reg' */
int jmptarget = 0; /* any code before this address is conditional */
@ -479,20 +471,19 @@ static int findsetreg (const Proto *p, int lastpc, int reg) {
/*
** Find a "name" for the constant 'c'.
*/
static const char *kname (const Proto *p, int index, const char **name) {
static const char *kname(const Proto *p, int index, const char **name) {
TValue *kvalue = &p->k[index];
if (ttisstring(kvalue)) {
*name = getstr(tsvalue(kvalue));
return "constant";
}
else {
} else {
*name = "?";
return NULL;
}
}
static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
static const char *basicgetobjname(const Proto *p, int *ppc, int reg,
const char **name) {
int pc = *ppc;
*name = luaF_getlocalname(p, reg + 1, pc);
@ -514,9 +505,12 @@ static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
*name = upvalname(p, GETARG_B(i));
return "upvalue";
}
case OP_LOADK: return kname(p, GETARG_Bx(i), name);
case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name);
default: break;
case OP_LOADK:
return kname(p, GETARG_Bx(i), name);
case OP_LOADKX:
return kname(p, GETARG_Ax(p->code[pc + 1]), name);
default:
break;
}
}
return NULL; /* could not find reasonable name */
@ -526,7 +520,7 @@ static const char *basicgetobjname (const Proto *p, int *ppc, int reg,
/*
** Find a "name" for the register 'c'.
*/
static void rname (const Proto *p, int pc, int c, const char **name) {
static void rname(const Proto *p, int pc, int c, const char **name) {
const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */
if (!(what && *what == 'c')) /* did not find a constant name? */
*name = "?";
@ -536,7 +530,7 @@ static void rname (const Proto *p, int pc, int c, const char **name) {
/*
** Find a "name" for a 'C' value in an RK instruction.
*/
static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
static void rkname(const Proto *p, int pc, Instruction i, const char **name) {
int c = GETARG_C(i); /* key index */
if (GETARG_k(i)) /* is 'c' a constant? */
kname(p, c, name);
@ -549,7 +543,7 @@ static void rkname (const Proto *p, int pc, Instruction i, const char **name) {
** Check whether table being indexed by instruction 'i' is the
** environment '_ENV'
*/
static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
static const char *isEnv(const Proto *p, int pc, Instruction i, int isup) {
int t = GETARG_B(i); /* table index */
const char *name; /* name of indexed variable */
if (isup) /* is 't' an upvalue? */
@ -563,7 +557,7 @@ static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) {
/*
** Extend 'basicgetobjname' to handle table accesses
*/
static const char *getobjname (const Proto *p, int lastpc, int reg,
static const char *getobjname(const Proto *p, int lastpc, int reg,
const char **name) {
const char *kind = basicgetobjname(p, &lastpc, reg, name);
if (kind != NULL)
@ -595,7 +589,8 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
rkname(p, lastpc, i, name);
return "method";
}
default: break; /* go through to return NULL */
default:
break; /* go through to return NULL */
}
}
return NULL; /* could not find reasonable name */
@ -608,7 +603,7 @@ static const char *getobjname (const Proto *p, int lastpc, int reg,
** Returns what the name is (e.g., "for iterator", "method",
** "metamethod") and sets '*name' to point to the name.
*/
static const char *funcnamefromcode (lua_State *L, const Proto *p,
static const char *funcnamefromcode(lua_State *L, const Proto *p,
int pc, const char **name) {
TMS tm = (TMS)0; /* (initial value avoids warnings) */
Instruction i = p->code[pc]; /* calling instruction */
@ -621,26 +616,55 @@ static const char *funcnamefromcode (lua_State *L, const Proto *p,
return "for iterator";
}
/* other instructions can do calls through metamethods */
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
case OP_GETI: case OP_GETFIELD:
case OP_SELF:
case OP_GETTABUP:
case OP_GETTABLE:
case OP_GETI:
case OP_GETFIELD:
tm = TM_INDEX;
break;
case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD:
case OP_SETTABUP:
case OP_SETTABLE:
case OP_SETI:
case OP_SETFIELD:
tm = TM_NEWINDEX;
break;
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
case OP_MMBIN:
case OP_MMBINI:
case OP_MMBINK: {
tm = cast(TMS, GETARG_C(i));
break;
}
case OP_UNM: tm = TM_UNM; break;
case OP_BNOT: tm = TM_BNOT; break;
case OP_LEN: tm = TM_LEN; break;
case OP_CONCAT: tm = TM_CONCAT; break;
case OP_EQ: tm = TM_EQ; break;
case OP_UNM:
tm = TM_UNM;
break;
case OP_BNOT:
tm = TM_BNOT;
break;
case OP_LEN:
tm = TM_LEN;
break;
case OP_CONCAT:
tm = TM_CONCAT;
break;
case OP_EQ:
tm = TM_EQ;
break;
/* no cases for OP_EQI and OP_EQK, as they don't call metamethods */
case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break;
case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break;
case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break;
case OP_LT:
case OP_LTI:
case OP_GTI:
tm = TM_LT;
break;
case OP_LE:
case OP_LEI:
case OP_GEI:
tm = TM_LE;
break;
case OP_CLOSE:
case OP_RETURN:
tm = TM_CLOSE;
break;
default:
return NULL; /* cannot find a reasonable name */
}
@ -652,17 +676,15 @@ static const char *funcnamefromcode (lua_State *L, const Proto *p,
/*
** Try to find a name for a function based on how it was called.
*/
static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
static const char *funcnamefromcall(lua_State *L, CallInfo *ci,
const char **name) {
if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */
*name = "?";
return "hook";
}
else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */
} else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */
*name = "__gc";
return "metamethod"; /* report it as such */
}
else if (isLua(ci))
} else if (isLua(ci))
return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name);
else
return NULL;
@ -678,7 +700,7 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
** not point to a value in this stack, we cannot compare it with the
** region boundaries (undefined behavior in ISO C).
*/
static int instack (CallInfo *ci, const TValue *o) {
static int instack(CallInfo *ci, const TValue *o) {
int pos;
StkId base = ci->func.p + 1;
for (pos = 0; base + pos < ci->top.p; pos++) {
@ -694,7 +716,7 @@ static int instack (CallInfo *ci, const TValue *o) {
** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on
** upvalues.)
*/
static const char *getupvalname (CallInfo *ci, const TValue *o,
static const char *getupvalname(CallInfo *ci, const TValue *o,
const char **name) {
LClosure *c = ci_func(ci);
int i;
@ -708,7 +730,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
}
static const char *formatvarinfo (lua_State *L, const char *kind,
static const char *formatvarinfo(lua_State *L, const char *kind,
const char *name) {
if (kind == NULL)
return ""; /* no information */
@ -720,7 +742,7 @@ static const char *formatvarinfo (lua_State *L, const char *kind,
** Build a string with a "description" for the value 'o', such as
** "variable 'x'" or "upvalue 'y'".
*/
static const char *varinfo (lua_State *L, const TValue *o) {
static const char *varinfo(lua_State *L, const TValue *o) {
CallInfo *ci = L->ci;
const char *name = NULL; /* to avoid warnings */
const char *kind = NULL;
@ -739,7 +761,7 @@ static const char *varinfo (lua_State *L, const TValue *o) {
/*
** Raise a type error
*/
static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
static l_noret typeerror(lua_State *L, const TValue *o, const char *op,
const char *extra) {
const char *t = luaT_objtypename(L, o);
luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra);
@ -750,7 +772,7 @@ static l_noret typeerror (lua_State *L, const TValue *o, const char *op,
** Raise a type error with "standard" information about the faulty
** object 'o' (using 'varinfo').
*/
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
l_noret luaG_typeerror(lua_State *L, const TValue *o, const char *op) {
typeerror(L, o, op, varinfo(L, o));
}
@ -760,7 +782,7 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
** for the object based on how it was called ('funcnamefromcall'); if it
** cannot get a name there, try 'varinfo'.
*/
l_noret luaG_callerror (lua_State *L, const TValue *o) {
l_noret luaG_callerror(lua_State *L, const TValue *o) {
CallInfo *ci = L->ci;
const char *name = NULL; /* to avoid warnings */
const char *kind = funcnamefromcall(L, ci, &name);
@ -769,19 +791,19 @@ l_noret luaG_callerror (lua_State *L, const TValue *o) {
}
l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) {
l_noret luaG_forerror(lua_State *L, const TValue *o, const char *what) {
luaG_runerror(L, "bad 'for' %s (number expected, got %s)",
what, luaT_objtypename(L, o));
}
l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
l_noret luaG_concaterror(lua_State *L, const TValue *p1, const TValue *p2) {
if (ttisstring(p1) || cvt2str(p1)) p1 = p2;
luaG_typeerror(L, p1, "concatenate");
}
l_noret luaG_opinterror (lua_State *L, const TValue *p1,
l_noret luaG_opinterror(lua_State *L, const TValue *p1,
const TValue *p2, const char *msg) {
if (!ttisnumber(p1)) /* first operand is wrong? */
p2 = p1; /* now second is wrong */
@ -792,7 +814,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1,
/*
** Error when both values are convertible to numbers, but not to integers
*/
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
l_noret luaG_tointerror(lua_State *L, const TValue *p1, const TValue *p2) {
lua_Integer temp;
if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
p2 = p1;
@ -800,7 +822,7 @@ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
}
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
l_noret luaG_ordererror(lua_State *L, const TValue *p1, const TValue *p2) {
const char *t1 = luaT_objtypename(L, p1);
const char *t2 = luaT_objtypename(L, p2);
if (strcmp(t1, t2) == 0)
@ -811,19 +833,20 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
/* add src:line information to 'msg' */
const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
const char *luaG_addinfo(lua_State *L, const char *msg, TString *src,
int line) {
char buff[LUA_IDSIZE];
if (src)
luaO_chunkid(buff, getstr(src), tsslen(src));
else { /* no source available; use "?" instead */
buff[0] = '?'; buff[1] = '\0';
buff[0] = '?';
buff[1] = '\0';
}
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
}
l_noret luaG_errormsg (lua_State *L) {
l_noret luaG_errormsg(lua_State *L) {
if (L->errfunc != 0) { /* is there an error handling function? */
StkId errfunc = restorestack(L, L->errfunc);
lua_assert(ttisfunction(s2v(errfunc)));
@ -836,7 +859,7 @@ l_noret luaG_errormsg (lua_State *L) {
}
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
l_noret luaG_runerror(lua_State *L, const char *fmt, ...) {
CallInfo *ci = L->ci;
const char *msg;
va_list argp;
@ -861,7 +884,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
** too far apart, there is a good chance of a ABSLINEINFO in the way,
** so it goes directly to 'luaG_getfuncline'.
*/
static int changedline (const Proto *p, int oldpc, int newpc) {
static int changedline(const Proto *p, int oldpc, int newpc) {
if (p->lineinfo == NULL) /* no debug information? */
return 0;
if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */
@ -890,7 +913,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) {
** a line/count hook before the call hook. Functions coming from
** an yield already called 'luaD_hookcall' before yielding.)
*/
int luaG_tracecall (lua_State *L) {
int luaG_tracecall(lua_State *L) {
CallInfo *ci = L->ci;
Proto *p = ci_func(ci)->p;
ci->u.l.trap = 1; /* ensure hooks will be checked */
@ -916,7 +939,7 @@ int luaG_tracecall (lua_State *L) {
** This function is not "Protected" when called, so it should correct
** 'L->top.p' before calling anything that can run the GC.
*/
int luaG_traceexec (lua_State *L, const Instruction *pc) {
int luaG_traceexec(lua_State *L, const Instruction *pc) {
CallInfo *ci = L->ci;
lu_byte mask = L->hookmask;
const Proto *p = ci_func(ci)->p;

View file

@ -36,29 +36,29 @@
#endif
LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc);
LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n,
LUAI_FUNC int luaG_getfuncline(const Proto *f, int pc);
LUAI_FUNC const char *luaG_findlocal(lua_State *L, CallInfo *ci, int n,
StkId *pos);
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
LUAI_FUNC l_noret luaG_typeerror(lua_State *L, const TValue *o,
const char *opname);
LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o);
LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o,
LUAI_FUNC l_noret luaG_callerror(lua_State *L, const TValue *o);
LUAI_FUNC l_noret luaG_forerror(lua_State *L, const TValue *o,
const char *what);
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
LUAI_FUNC l_noret luaG_concaterror(lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
LUAI_FUNC l_noret luaG_opinterror(lua_State *L, const TValue *p1,
const TValue *p2,
const char *msg);
LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
LUAI_FUNC l_noret luaG_tointerror(lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
LUAI_FUNC l_noret luaG_ordererror(lua_State *L, const TValue *p1,
const TValue *p2);
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
LUAI_FUNC l_noret luaG_runerror(lua_State *L, const char *fmt, ...);
LUAI_FUNC const char *luaG_addinfo(lua_State *L, const char *msg,
TString *src, int line);
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
LUAI_FUNC int luaG_tracecall (lua_State *L);
LUAI_FUNC l_noret luaG_errormsg(lua_State *L);
LUAI_FUNC int luaG_traceexec(lua_State *L, const Instruction *pc);
LUAI_FUNC int luaG_tracecall(lua_State *L);
#endif

View file

@ -88,7 +88,7 @@ struct lua_longjmp {
};
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
void luaD_seterrorobj(lua_State *L, int errcode, StkId oldtop) {
switch (errcode) {
case LUA_ERRMEM: { /* memory error? */
setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
@ -112,19 +112,17 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
}
l_noret luaD_throw (lua_State *L, int errcode) {
l_noret luaD_throw(lua_State *L, int errcode) {
if (L->errorJmp) { /* thread has an error handler? */
L->errorJmp->status = errcode; /* set status */
LUAI_THROW(L, L->errorJmp); /* jump to it */
}
else { /* thread has no error handler */
} else { /* thread has no error handler */
global_State *g = G(L);
errcode = luaE_resetthread(L, errcode); /* close all upvalues */
if (g->mainthread->errorJmp) { /* main thread has a handler? */
setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */
luaD_throw(g->mainthread, errcode); /* re-throw in main thread */
}
else { /* no handler at all; abort */
} else { /* no handler at all; abort */
if (g->panic) { /* panic function? */
lua_unlock(L);
g->panic(L); /* call panic function (last chance to jump out) */
@ -135,7 +133,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
}
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud) {
l_uint32 oldnCcalls = L->nCcalls;
struct lua_longjmp lj;
lj.status = LUA_OK;
@ -162,7 +160,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
/*
** Change all pointers to the stack into offsets.
*/
static void relstack (lua_State *L) {
static void relstack(lua_State *L) {
CallInfo *ci;
UpVal *up;
L->top.offset = savestack(L, L->top.p);
@ -179,7 +177,7 @@ static void relstack (lua_State *L) {
/*
** Change back all offsets into pointers.
*/
static void correctstack (lua_State *L) {
static void correctstack(lua_State *L) {
CallInfo *ci;
UpVal *up;
L->top.p = restorestack(L, L->top.offset);
@ -209,7 +207,7 @@ static void correctstack (lua_State *L) {
** In case of allocation error, raise an error or return false according
** to 'raiseerror'.
*/
int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
int luaD_reallocstack(lua_State *L, int newsize, int raiseerror) {
int oldsize = stacksize(L);
int i;
StkId newstack;
@ -239,7 +237,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
** Try to grow the stack by at least 'n' elements. When 'raiseerror'
** is true, raises any error; otherwise, return 0 in case of errors.
*/
int luaD_growstack (lua_State *L, int n, int raiseerror) {
int luaD_growstack(lua_State *L, int n, int raiseerror) {
int size = stacksize(L);
if (l_unlikely(size > LUAI_MAXSTACK)) {
/* if stack is larger than maximum, thread is already using the
@ -249,8 +247,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) {
if (raiseerror)
luaD_throw(L, LUA_ERRERR); /* error inside message handler */
return 0; /* if not 'raiseerror', just signal it */
}
else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */
} else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */
int newsize = 2 * size; /* tentative new size */
int needed = cast_int(L->top.p - L->stack.p) + n;
if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */
@ -273,7 +270,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) {
** Compute how much of the stack is being used, by computing the
** maximum top of all call frames in the stack and the current top.
*/
static int stackinuse (lua_State *L) {
static int stackinuse(lua_State *L) {
CallInfo *ci;
int res;
StkId lim = L->top.p;
@ -297,7 +294,7 @@ static int stackinuse (lua_State *L) {
** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack
** will be reduced to a "regular" size.
*/
void luaD_shrinkstack (lua_State *L) {
void luaD_shrinkstack(lua_State *L) {
int inuse = stackinuse(L);
int max = (inuse > LUAI_MAXSTACK / 3) ? LUAI_MAXSTACK : inuse * 3;
/* if thread is currently not handling a stack overflow and its
@ -305,14 +302,13 @@ void luaD_shrinkstack (lua_State *L) {
if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) {
int nsize = (inuse > LUAI_MAXSTACK / 2) ? LUAI_MAXSTACK : inuse * 2;
luaD_reallocstack(L, nsize, 0); /* ok if that fails */
}
else /* don't change stack */
condmovestack(L,{},{}); /* (change only for debugging) */
} else /* don't change stack */
condmovestack(L, {}, {}); /* (change only for debugging) */
luaE_shrinkCI(L); /* shrink CI list */
}
void luaD_inctop (lua_State *L) {
void luaD_inctop(lua_State *L) {
luaD_checkstack(L, 1);
L->top.p++;
}
@ -325,7 +321,7 @@ void luaD_inctop (lua_State *L) {
** called. (Both 'L->hook' and 'L->hookmask', which trigger this
** function, can be changed asynchronously by signals.)
*/
void luaD_hook (lua_State *L, int event, int line,
void luaD_hook(lua_State *L, int event, int line,
int ftransfer, int ntransfer) {
lua_Hook hook = L->hook;
if (hook && L->allowhook) { /* make sure there is a hook */
@ -366,7 +362,7 @@ void luaD_hook (lua_State *L, int event, int line,
** whenever 'hookmask' is not zero, so it checks whether call hooks are
** active.
*/
void luaD_hookcall (lua_State *L, CallInfo *ci) {
void luaD_hookcall(lua_State *L, CallInfo *ci) {
L->oldpc = 0; /* set 'oldpc' for new function */
if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */
int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL
@ -384,7 +380,7 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) {
** 'oldpc'. (Note that this correction is needed by the line hook, so it
** is done even when return hooks are off.)
*/
static void rethook (lua_State *L, CallInfo *ci, int nres) {
static void rethook(lua_State *L, CallInfo *ci, int nres) {
if (L->hookmask & LUA_MASKRET) { /* is return hook on? */
StkId firstres = L->top.p - nres; /* index of first result */
int delta = 0; /* correction for vararg functions */
@ -409,7 +405,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) {
** stack, below original 'func', so that 'luaD_precall' can call it. Raise
** an error if there is no '__call' metafield.
*/
static StkId tryfuncTM (lua_State *L, StkId func) {
static StkId tryfuncTM(lua_State *L, StkId func) {
const TValue *tm;
StkId p;
checkstackGCp(L, 1, func); /* space for metamethod */
@ -417,7 +413,7 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
if (l_unlikely(ttisnil(tm)))
luaG_callerror(L, s2v(func)); /* nothing to call */
for (p = L->top.p; p > func; p--) /* open space for metamethod */
setobjs2s(L, p, p-1);
setobjs2s(L, p, p - 1);
L->top.p++; /* stack space pre-allocated by the caller */
setobj2s(L, func, tm); /* metamethod is the new function to be called */
return func;
@ -430,7 +426,7 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
** expressions, multiple results for tail calls/single parameters)
** separated.
*/
l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
l_sinline void moveresults(lua_State *L, StkId res, int nres, int wanted) {
StkId firstresult;
int i;
switch (wanted) { /* handle typical cases separately */
@ -482,7 +478,7 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
** info. If function has to close variables, hook must be called after
** that.
*/
void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
void luaD_poscall(lua_State *L, CallInfo *ci, int nres) {
int wanted = ci->nresults;
if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted)))
rethook(L, ci, nres);
@ -499,7 +495,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret,
l_sinline CallInfo *prepCallInfo(lua_State *L, StkId func, int nret,
int mask, StkId top) {
CallInfo *ci = L->ci = next_ci(L); /* new frame */
ci->func.p = func;
@ -513,7 +509,7 @@ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret,
/*
** precall for C functions
*/
l_sinline int precallC (lua_State *L, StkId func, int nresults,
l_sinline int precallC(lua_State *L, StkId func, int nresults,
lua_CFunction f) {
int n; /* number of returns */
CallInfo *ci;
@ -540,9 +536,9 @@ l_sinline int precallC (lua_State *L, StkId func, int nresults,
** (so that it includes the function itself). Return the number of
** results, if it was a C function, or -1 for a Lua function.
*/
int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
int luaD_pretailcall(lua_State *L, CallInfo *ci, StkId func,
int narg1, int delta) {
retry:
retry:
switch (ttypetag(s2v(func))) {
case LUA_VCCL: /* C closure */
return precallC(L, func, LUA_MULTRET, clCvalue(s2v(func))->f);
@ -585,8 +581,8 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
** returns NULL, with all the results on the stack, starting at the
** original function position.
*/
CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
retry:
CallInfo *luaD_precall(lua_State *L, StkId func, int nresults) {
retry:
switch (ttypetag(s2v(func))) {
case LUA_VCCL: /* C closure */
precallC(L, func, nresults, clCvalue(s2v(func))->f);
@ -625,7 +621,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
** check the stack before doing anything else. 'luaD_precall' already
** does that.
*/
l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) {
l_sinline void ccall(lua_State *L, StkId func, int nResults, l_uint32 inc) {
CallInfo *ci;
L->nCcalls += inc;
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) {
@ -643,7 +639,7 @@ l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) {
/*
** External interface for 'ccall'
*/
void luaD_call (lua_State *L, StkId func, int nResults) {
void luaD_call(lua_State *L, StkId func, int nResults) {
ccall(L, func, nResults, 1);
}
@ -651,7 +647,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
/*
** Similar to 'luaD_call', but does not allow yields during the call.
*/
void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
void luaD_callnoyield(lua_State *L, StkId func, int nResults) {
ccall(L, func, nResults, nyci);
}
@ -672,7 +668,7 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
** particular, field CIST_RECST preserves the error status across these
** multiple runs, changing only if there is a new error.
*/
static int finishpcallk (lua_State *L, CallInfo *ci) {
static int finishpcallk(lua_State *L, CallInfo *ci) {
int status = getcistrecst(ci); /* get original status */
if (l_likely(status == LUA_OK)) /* no error? */
status = LUA_YIELD; /* was interrupted by an yield */
@ -705,14 +701,13 @@ static int finishpcallk (lua_State *L, CallInfo *ci) {
** of the function called by 'lua_callk'/'lua_pcallk', so we are
** conservative and use LUA_MULTRET (always adjust).
*/
static void finishCcall (lua_State *L, CallInfo *ci) {
static void finishCcall(lua_State *L, CallInfo *ci) {
int n; /* actual number of results from C function */
if (ci->callstatus & CIST_CLSRET) { /* was returning? */
lua_assert(hastocloseCfunc(ci->nresults));
n = ci->u2.nres; /* just redo 'luaD_poscall' */
/* don't need to reset CIST_CLSRET, as it will be set again anyway */
}
else {
} else {
int status = LUA_YIELD; /* default if there were no errors */
/* must have a continuation and must be able to call it */
lua_assert(ci->u.c.k != NULL && yieldable(L));
@ -733,7 +728,7 @@ static void finishCcall (lua_State *L, CallInfo *ci) {
** previously interrupted coroutine until the stack is empty (or another
** interruption long-jumps out of the loop).
*/
static void unroll (lua_State *L, void *ud) {
static void unroll(lua_State *L, void *ud) {
CallInfo *ci;
UNUSED(ud);
while ((ci = L->ci) != &L->base_ci) { /* something in the stack */
@ -751,7 +746,7 @@ static void unroll (lua_State *L, void *ud) {
** Try to find a suspended protected call (a "recover point") for the
** given thread.
*/
static CallInfo *findpcall (lua_State *L) {
static CallInfo *findpcall(lua_State *L) {
CallInfo *ci;
for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
if (ci->callstatus & CIST_YPCALL)
@ -766,7 +761,7 @@ static CallInfo *findpcall (lua_State *L) {
** of the coroutine itself. (Such errors should not be handled by any
** coroutine error handler and should not kill the coroutine.)
*/
static int resume_error (lua_State *L, const char *msg, int narg) {
static int resume_error(lua_State *L, const char *msg, int narg) {
L->top.p -= narg; /* remove args from the stack */
setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */
api_incr_top(L);
@ -782,8 +777,8 @@ static int resume_error (lua_State *L, const char *msg, int narg) {
** function), plus erroneous cases: non-suspended coroutine or dead
** coroutine.
*/
static void resume (lua_State *L, void *ud) {
int n = *(cast(int*, ud)); /* number of arguments */
static void resume(lua_State *L, void *ud) {
int n = *(cast(int *, ud)); /* number of arguments */
StkId firstArg = L->top.p - n; /* first argument */
CallInfo *ci = L->ci;
if (L->status == LUA_OK) /* starting a coroutine? */
@ -798,8 +793,7 @@ static void resume (lua_State *L, void *ud) {
ci->u.l.savedpc--;
L->top.p = firstArg; /* discard arguments */
luaV_execute(L, ci); /* just continue running Lua code */
}
else { /* 'common' yield */
} else { /* 'common' yield */
if (ci->u.c.k != NULL) { /* does it have a continuation function? */
lua_unlock(L);
n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
@ -821,7 +815,7 @@ static void resume (lua_State *L, void *ud) {
** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't
** find a recover point).
*/
static int precover (lua_State *L, int status) {
static int precover(lua_State *L, int status) {
CallInfo *ci;
while (errorstatus(status) && (ci = findpcall(L)) != NULL) {
L->ci = ci; /* go down to recovery functions */
@ -832,7 +826,7 @@ static int precover (lua_State *L, int status) {
}
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
LUA_API int lua_resume(lua_State *L, lua_State *from, int nargs,
int *nresults) {
int status;
lua_lock(L);
@ -841,8 +835,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
return resume_error(L, "cannot resume non-suspended coroutine", nargs);
else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */
return resume_error(L, "cannot resume dead coroutine", nargs);
}
else if (L->status != LUA_YIELD) /* ended with errors? */
} else if (L->status != LUA_YIELD) /* ended with errors? */
return resume_error(L, "cannot resume dead coroutine", nargs);
L->nCcalls = (from) ? getCcalls(from) : 0;
if (getCcalls(L) >= LUAI_MAXCCALLS)
@ -867,12 +860,12 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
}
LUA_API int lua_isyieldable (lua_State *L) {
LUA_API int lua_isyieldable(lua_State *L) {
return yieldable(L);
}
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
LUA_API int lua_yieldk(lua_State *L, int nresults, lua_KContext ctx,
lua_KFunction k) {
CallInfo *ci;
luai_userstateyield(L, nresults);
@ -891,8 +884,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
lua_assert(!isLuacode(ci));
api_check(L, nresults == 0, "hooks cannot yield values");
api_check(L, k == NULL, "hooks cannot continue after yielding");
}
else {
} else {
if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
ci->u.c.ctx = ctx; /* save context */
luaD_throw(L, LUA_YIELD);
@ -915,7 +907,7 @@ struct CloseP {
/*
** Auxiliary function to call 'luaF_close' in protected mode.
*/
static void closepaux (lua_State *L, void *ud) {
static void closepaux(lua_State *L, void *ud) {
struct CloseP *pcl = cast(struct CloseP *, ud);
luaF_close(L, pcl->level, pcl->status, 0);
}
@ -925,12 +917,13 @@ static void closepaux (lua_State *L, void *ud) {
** Calls 'luaF_close' in protected mode. Return the original status
** or, in case of errors, the new status.
*/
int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
int luaD_closeprotected(lua_State *L, ptrdiff_t level, int status) {
CallInfo *old_ci = L->ci;
lu_byte old_allowhooks = L->allowhook;
for (;;) { /* keep closing upvalues until no more errors */
struct CloseP pcl;
pcl.level = restorestack(L, level); pcl.status = status;
pcl.level = restorestack(L, level);
pcl.status = status;
status = luaD_rawrunprotected(L, &closepaux, &pcl);
if (l_likely(status == LUA_OK)) /* no more errors? */
return pcl.status;
@ -947,7 +940,7 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) {
** thread information ('allowhook', etc.) and in particular
** its stack level in case of errors.
*/
int luaD_pcall (lua_State *L, Pfunc func, void *u,
int luaD_pcall(lua_State *L, Pfunc func, void *u,
ptrdiff_t old_top, ptrdiff_t ef) {
int status;
CallInfo *old_ci = L->ci;
@ -980,7 +973,7 @@ struct SParser { /* data to 'f_parser' */
};
static void checkmode (lua_State *L, const char *mode, const char *x) {
static void checkmode(lua_State *L, const char *mode, const char *x) {
if (mode && strchr(mode, x[0]) == NULL) {
luaO_pushfstring(L,
"attempt to load a %s chunk (mode is '%s')", x, mode);
@ -989,15 +982,14 @@ static void checkmode (lua_State *L, const char *mode, const char *x) {
}
static void f_parser (lua_State *L, void *ud) {
static void f_parser(lua_State *L, void *ud) {
LClosure *cl;
struct SParser *p = cast(struct SParser *, ud);
int c = zgetc(p->z); /* read first character */
if (c == LUA_SIGNATURE[0]) {
checkmode(L, p->mode, "binary");
cl = luaU_undump(L, p->z, p->name);
}
else {
} else {
checkmode(L, p->mode, "text");
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
}
@ -1006,15 +998,20 @@ static void f_parser (lua_State *L, void *ud) {
}
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
int luaD_protectedparser(lua_State *L, ZIO *z, const char *name,
const char *mode) {
struct SParser p;
int status;
incnny(L); /* cannot yield during parsing */
p.z = z; p.name = name; p.mode = mode;
p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
p.dyd.label.arr = NULL; p.dyd.label.size = 0;
p.z = z;
p.name = name;
p.mode = mode;
p.dyd.actvar.arr = NULL;
p.dyd.actvar.size = 0;
p.dyd.gt.arr = NULL;
p.dyd.gt.size = 0;
p.dyd.label.arr = NULL;
p.dyd.label.size = 0;
luaZ_initbuffer(L, &p.buff);
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc);
luaZ_freebuffer(L, &p.buff);

View file

@ -58,30 +58,30 @@
/* type of protected functions, to be ran by 'runprotected' */
typedef void (*Pfunc) (lua_State *L, void *ud);
typedef void (*Pfunc)(lua_State *L, void *ud);
LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
LUAI_FUNC void luaD_seterrorobj(lua_State *L, int errcode, StkId oldtop);
LUAI_FUNC int luaD_protectedparser(lua_State *L, ZIO *z, const char *name,
const char *mode);
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
LUAI_FUNC void luaD_hook(lua_State *L, int event, int line,
int fTransfer, int nTransfer);
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
LUAI_FUNC void luaD_hookcall(lua_State *L, CallInfo *ci);
LUAI_FUNC int luaD_pretailcall(lua_State *L, CallInfo *ci, StkId func,
int narg1, int delta);
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
LUAI_FUNC CallInfo *luaD_precall(lua_State *L, StkId func, int nResults);
LUAI_FUNC void luaD_call(lua_State *L, StkId func, int nResults);
LUAI_FUNC void luaD_callnoyield(lua_State *L, StkId func, int nResults);
LUAI_FUNC int luaD_closeprotected(lua_State *L, ptrdiff_t level, int status);
LUAI_FUNC int luaD_pcall(lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
LUAI_FUNC void luaD_inctop (lua_State *L);
LUAI_FUNC void luaD_poscall(lua_State *L, CallInfo *ci, int nres);
LUAI_FUNC int luaD_reallocstack(lua_State *L, int newsize, int raiseerror);
LUAI_FUNC int luaD_growstack(lua_State *L, int n, int raiseerror);
LUAI_FUNC void luaD_shrinkstack(lua_State *L);
LUAI_FUNC void luaD_inctop(lua_State *L);
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
LUAI_FUNC l_noret luaD_throw(lua_State *L, int errcode);
LUAI_FUNC int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud);
#endif

View file

@ -38,7 +38,7 @@ typedef struct {
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
static void dumpBlock (DumpState *D, const void *b, size_t size) {
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);
@ -50,7 +50,7 @@ static void dumpBlock (DumpState *D, const void *b, size_t size) {
#define dumpVar(D,x) dumpVector(D,&x,1)
static void dumpByte (DumpState *D, int y) {
static void dumpByte(DumpState *D, int y) {
lu_byte x = (lu_byte)y;
dumpVar(D, x);
}
@ -62,7 +62,7 @@ static void dumpByte (DumpState *D, int y) {
*/
#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7)
static void dumpSize (DumpState *D, size_t x) {
static void dumpSize(DumpState *D, size_t x) {
lu_byte buff[DIBS];
int n = 0;
do {
@ -74,22 +74,22 @@ static void dumpSize (DumpState *D, size_t x) {
}
static void dumpInt (DumpState *D, int x) {
static void dumpInt(DumpState *D, int x) {
dumpSize(D, x);
}
static void dumpNumber (DumpState *D, lua_Number x) {
static void dumpNumber(DumpState *D, lua_Number x) {
dumpVar(D, x);
}
static void dumpInteger (DumpState *D, lua_Integer x) {
static void dumpInteger(DumpState *D, lua_Integer x) {
dumpVar(D, x);
}
static void dumpString (DumpState *D, const TString *s) {
static void dumpString(DumpState *D, const TString *s) {
if (s == NULL)
dumpSize(D, 0);
else {
@ -101,7 +101,7 @@ static void dumpString (DumpState *D, const TString *s) {
}
static void dumpCode (DumpState *D, const Proto *f) {
static void dumpCode(DumpState *D, const Proto *f) {
dumpInt(D, f->sizecode);
dumpVector(D, f->code, f->sizecode);
}
@ -109,7 +109,7 @@ static void dumpCode (DumpState *D, const Proto *f) {
static void dumpFunction(DumpState *D, const Proto *f, TString *psource);
static void dumpConstants (DumpState *D, const Proto *f) {
static void dumpConstants(DumpState *D, const Proto *f) {
int i;
int n = f->sizek;
dumpInt(D, n);
@ -135,7 +135,7 @@ static void dumpConstants (DumpState *D, const Proto *f) {
}
static void dumpProtos (DumpState *D, const Proto *f) {
static void dumpProtos(DumpState *D, const Proto *f) {
int i;
int n = f->sizep;
dumpInt(D, n);
@ -144,7 +144,7 @@ static void dumpProtos (DumpState *D, const Proto *f) {
}
static void dumpUpvalues (DumpState *D, const Proto *f) {
static void dumpUpvalues(DumpState *D, const Proto *f) {
int i, n = f->sizeupvalues;
dumpInt(D, n);
for (i = 0; i < n; i++) {
@ -155,7 +155,7 @@ static void dumpUpvalues (DumpState *D, const Proto *f) {
}
static void dumpDebug (DumpState *D, const Proto *f) {
static void dumpDebug(DumpState *D, const Proto *f) {
int i, n;
n = (D->strip) ? 0 : f->sizelineinfo;
dumpInt(D, n);
@ -180,7 +180,7 @@ static void dumpDebug (DumpState *D, const Proto *f) {
}
static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
static void dumpFunction(DumpState *D, const Proto *f, TString *psource) {
if (D->strip || f->source == psource)
dumpString(D, NULL); /* no debug info or same source as its parent */
else
@ -198,7 +198,7 @@ static void dumpFunction (DumpState *D, const Proto *f, TString *psource) {
}
static void dumpHeader (DumpState *D) {
static void dumpHeader(DumpState *D) {
dumpLiteral(D, LUA_SIGNATURE);
dumpByte(D, LUAC_VERSION);
dumpByte(D, LUAC_FORMAT);

View file

@ -24,7 +24,7 @@
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
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(nupvals);
@ -32,7 +32,7 @@ CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
}
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
LClosure *luaF_newLclosure(lua_State *L, int nupvals) {
GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
LClosure *c = gco2lcl(o);
c->p = NULL;
@ -45,7 +45,7 @@ LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
/*
** fill a closure with new closed upvalues
*/
void luaF_initupvals (lua_State *L, LClosure *cl) {
void luaF_initupvals(lua_State *L, LClosure *cl) {
int i;
for (i = 0; i < cl->nupvalues; i++) {
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
@ -62,7 +62,7 @@ void luaF_initupvals (lua_State *L, LClosure *cl) {
** Create a new upvalue at the given level, and link it to the list of
** open upvalues of 'L' after entry 'prev'.
**/
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
static UpVal *newupval(lua_State *L, StkId level, UpVal **prev) {
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
UpVal *uv = gco2upv(o);
UpVal *next = *prev;
@ -84,7 +84,7 @@ static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
** Find and reuse, or create if it does not exist, an upvalue
** at the given level.
*/
UpVal *luaF_findupval (lua_State *L, StkId level) {
UpVal *luaF_findupval(lua_State *L, StkId level) {
UpVal **pp = &L->openupval;
UpVal *p;
lua_assert(isintwups(L) || L->openupval == NULL);
@ -104,7 +104,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) {
** boolean 'yy' controls whether the call is yieldable.
** (This function assumes EXTRA_STACK.)
*/
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
static void callclosemethod(lua_State *L, TValue *obj, TValue *err, int yy) {
StkId top = L->top.p;
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
setobj2s(L, top, tm); /* will call metamethod... */
@ -122,7 +122,7 @@ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
** Check whether object at given level has a close metamethod and raise
** an error if not.
*/
static void checkclosemth (lua_State *L, StkId level) {
static void checkclosemth(lua_State *L, StkId level) {
const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
if (ttisnil(tm)) { /* no metamethod? */
int idx = cast_int(level - L->ci->func.p); /* variable index */
@ -140,7 +140,7 @@ static void checkclosemth (lua_State *L, StkId level) {
** the 'level' of the upvalue being closed, as everything after that
** won't be used again.
*/
static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
static void prepcallclosemth(lua_State *L, StkId level, int status, int yy) {
TValue *uv = s2v(level); /* value being closed */
TValue *errobj;
if (status == CLOSEKTOP)
@ -165,7 +165,7 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
/*
** Insert a variable in the list of to-be-closed variables.
*/
void luaF_newtbcupval (lua_State *L, StkId level) {
void luaF_newtbcupval(lua_State *L, StkId level) {
lua_assert(level > L->tbclist.p);
if (l_isfalse(s2v(level)))
return; /* false doesn't need to be closed */
@ -179,7 +179,7 @@ void luaF_newtbcupval (lua_State *L, StkId level) {
}
void luaF_unlinkupval (UpVal *uv) {
void luaF_unlinkupval(UpVal *uv) {
lua_assert(upisopen(uv));
*uv->u.open.previous = uv->u.open.next;
if (uv->u.open.next)
@ -190,7 +190,7 @@ void luaF_unlinkupval (UpVal *uv) {
/*
** Close all upvalues up to the given stack level.
*/
void luaF_closeupval (lua_State *L, StkId level) {
void luaF_closeupval(lua_State *L, StkId level) {
UpVal *uv;
StkId upl; /* stack index pointed by 'uv' */
while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
@ -210,7 +210,7 @@ void luaF_closeupval (lua_State *L, StkId level) {
/*
** Remove first element from the tbclist plus its dummy nodes.
*/
static void poptbclist (lua_State *L) {
static void poptbclist(lua_State *L) {
StkId tbc = L->tbclist.p;
lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
tbc -= tbc->tbclist.delta;
@ -224,7 +224,7 @@ static void poptbclist (lua_State *L) {
** Close all upvalues and to-be-closed variables up to the given stack
** level. Return restored 'level'.
*/
StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
StkId luaF_close(lua_State *L, StkId level, int status, int yy) {
ptrdiff_t levelrel = savestack(L, level);
luaF_closeupval(L, level); /* first, close the upvalues */
while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
@ -237,7 +237,7 @@ StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
}
Proto *luaF_newproto (lua_State *L) {
Proto *luaF_newproto(lua_State *L) {
GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
Proto *f = gco2p(o);
f->k = NULL;
@ -264,7 +264,7 @@ Proto *luaF_newproto (lua_State *L) {
}
void luaF_freeproto (lua_State *L, Proto *f) {
void luaF_freeproto(lua_State *L, Proto *f) {
luaM_freearray(L, f->code, f->sizecode);
luaM_freearray(L, f->p, f->sizep);
luaM_freearray(L, f->k, f->sizek);
@ -280,9 +280,9 @@ void luaF_freeproto (lua_State *L, Proto *f) {
** Look for n-th local variable at line 'line' in function 'func'.
** Returns NULL if not found.
*/
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
const char *luaF_getlocalname(const Proto *f, int local_number, int pc) {
int i;
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
for (i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
if (pc < f->locvars[i].endpc) { /* is variable active? */
local_number--;
if (local_number == 0)

View file

@ -47,17 +47,17 @@
#define CLOSEKTOP (-1)
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
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);
LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level);
LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy);
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
LUAI_FUNC Proto *luaF_newproto(lua_State *L);
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);
LUAI_FUNC void luaF_closeupval(lua_State *L, StkId level);
LUAI_FUNC StkId luaF_close(lua_State *L, StkId level, int status, int yy);
LUAI_FUNC void luaF_unlinkupval(UpVal *uv);
LUAI_FUNC void luaF_freeproto(lua_State *L, Proto *f);
LUAI_FUNC const char *luaF_getlocalname(const Proto *func, int local_number,
int pc);

View file

@ -104,9 +104,9 @@
*/
#define markobjectN(g,t) { if (t) markobject(g,t); }
static void reallymarkobject (global_State *g, GCObject *o);
static lu_mem atomic (lua_State *L);
static void entersweep (lua_State *L);
static void reallymarkobject(global_State *g, GCObject *o);
static lu_mem atomic(lua_State *L);
static void entersweep(lua_State *L);
/*
@ -122,19 +122,26 @@ static void entersweep (lua_State *L);
#define gnodelast(h) gnode(h, cast_sizet(sizenode(h)))
static GCObject **getgclist (GCObject *o) {
static GCObject **getgclist(GCObject *o) {
switch (o->tt) {
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_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;
}
default: lua_assert(0); return 0;
default:
lua_assert(0);
return 0;
}
}
@ -145,7 +152,7 @@ static GCObject **getgclist (GCObject *o) {
*/
#define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p))
static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) {
static void linkgclist_(GCObject *o, GCObject **pnext, GCObject **list) {
lua_assert(!isgray(o)); /* cannot be in a gray list */
*pnext = *list;
*list = o;
@ -168,7 +175,7 @@ static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) {
** its associated empty value is enough to signal that the entry is
** logically empty.
*/
static void clearkey (Node *n) {
static void clearkey(Node *n) {
lua_assert(isempty(gval(n)));
if (keyiscollectable(n))
setdeadkey(n); /* unused key; remove it */
@ -182,13 +189,12 @@ static void clearkey (Node *n) {
** other objects: if really collected, cannot keep them; for objects
** being finalized, keep them in keys, but not in values
*/
static int iscleared (global_State *g, const GCObject *o) {
static int iscleared(global_State *g, const GCObject *o) {
if (o == NULL) return 0; /* non-collectable value */
else if (novariant(o->tt) == LUA_TSTRING) {
markobject(g, o); /* strings are 'values', so are never weak */
return 0;
}
else return iswhite(o);
} else return iswhite(o);
}
@ -205,7 +211,7 @@ static int iscleared (global_State *g, const GCObject *o) {
** be done is generational mode, as its sweep does not distinguish
** whites from deads.)
*/
void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v) {
global_State *g = G(L);
lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
if (keepinvariant(g)) { /* must keep invariant? */
@ -214,8 +220,7 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
lua_assert(!isold(v)); /* white object could not be old */
setage(v, G_OLD0); /* restore generational invariant */
}
}
else { /* sweep phase */
} else { /* sweep phase */
lua_assert(issweepphase(g));
if (g->gckind == KGC_INC) /* incremental mode? */
makewhite(g, o); /* mark 'o' as white to avoid other barriers */
@ -227,7 +232,7 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
** barrier that moves collector backward, that is, mark the black object
** pointing to a white object as gray again.
*/
void luaC_barrierback_ (lua_State *L, GCObject *o) {
void luaC_barrierback_(lua_State *L, GCObject *o) {
global_State *g = G(L);
lua_assert(isblack(o) && !isdead(g, o));
lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1));
@ -240,7 +245,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) {
}
void luaC_fix (lua_State *L, GCObject *o) {
void luaC_fix(lua_State *L, GCObject *o) {
global_State *g = G(L);
lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */
set2gray(o); /* they will be gray forever */
@ -255,7 +260,7 @@ void luaC_fix (lua_State *L, GCObject *o) {
** create a new collectable object (with given type, size, and offset)
** and link it to 'allgc' list.
*/
GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
GCObject *luaC_newobjdt(lua_State *L, int tt, size_t sz, size_t offset) {
global_State *g = G(L);
char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
GCObject *o = cast(GCObject *, p + offset);
@ -267,7 +272,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
}
GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
GCObject *luaC_newobj(lua_State *L, int tt, size_t sz) {
return luaC_newobjdt(L, tt, sz, 0);
}
@ -294,7 +299,7 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
** for at most two levels: An upvalue cannot refer to another upvalue
** (only closures can), and a userdata's metatable must be a table.
*/
static void reallymarkobject (global_State *g, GCObject *o) {
static void reallymarkobject(global_State *g, GCObject *o) {
switch (o->tt) {
case LUA_VSHRSTR:
case LUA_VLNGSTR: {
@ -319,12 +324,17 @@ static void reallymarkobject (global_State *g, GCObject *o) {
}
/* else... */
} /* FALLTHROUGH */
case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE:
case LUA_VTHREAD: case LUA_VPROTO: {
case LUA_VLCL:
case LUA_VCCL:
case LUA_VTABLE:
case LUA_VTHREAD:
case LUA_VPROTO: {
linkobjgclist(o, g->gray); /* to be visited later */
break;
}
default: lua_assert(0); break;
default:
lua_assert(0);
break;
}
}
@ -332,9 +342,9 @@ static void reallymarkobject (global_State *g, GCObject *o) {
/*
** mark metamethods for basic types
*/
static void markmt (global_State *g) {
static void markmt(global_State *g) {
int i;
for (i=0; i < LUA_NUMTAGS; i++)
for (i = 0; i < LUA_NUMTAGS; i++)
markobjectN(g, g->mt[i]);
}
@ -342,7 +352,7 @@ static void markmt (global_State *g) {
/*
** mark all objects in list of being-finalized
*/
static lu_mem markbeingfnz (global_State *g) {
static lu_mem markbeingfnz(global_State *g) {
GCObject *o;
lu_mem count = 0;
for (o = g->tobefnz; o != NULL; o = o->next) {
@ -364,7 +374,7 @@ static lu_mem markbeingfnz (global_State *g) {
** upvalues, as they have nothing to be checked. (If the thread gets an
** upvalue later, it will be linked in the list again.)
*/
static int remarkupvals (global_State *g) {
static int remarkupvals(global_State *g) {
lua_State *thread;
lua_State **p = &g->twups;
int work = 0; /* estimate of how much work was done here */
@ -391,7 +401,7 @@ static int remarkupvals (global_State *g) {
}
static void cleargraylists (global_State *g) {
static void cleargraylists(global_State *g) {
g->gray = g->grayagain = NULL;
g->weak = g->allweak = g->ephemeron = NULL;
}
@ -400,7 +410,7 @@ static void cleargraylists (global_State *g) {
/*
** mark root set and reset all gray lists, to start a new collection
*/
static void restartcollection (global_State *g) {
static void restartcollection(global_State *g) {
cleargraylists(g);
markobject(g, g->mainthread);
markvalue(g, &g->l_registry);
@ -427,7 +437,7 @@ static void restartcollection (global_State *g) {
** back to a gray list, but then it must become OLD. (That is what
** 'correctgraylist' does when it finds a TOUCHED2 object.)
*/
static void genlink (global_State *g, GCObject *o) {
static void genlink(global_State *g, GCObject *o) {
lua_assert(isblack(o));
if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */
linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */
@ -443,7 +453,7 @@ static void genlink (global_State *g, GCObject *o) {
** atomic phase. In the atomic phase, if table has any white value,
** put it in 'weak' list, to be cleared.
*/
static void traverseweakvalue (global_State *g, Table *h) {
static void traverseweakvalue(global_State *g, Table *h) {
Node *n, *limit = gnodelast(h);
/* if there is array part, assume it may have white values (it is not
worth traversing it now just to check) */
@ -477,7 +487,7 @@ static void traverseweakvalue (global_State *g, Table *h) {
** must be kept in some gray list for post-processing; this is done
** by 'genlink'.
*/
static int traverseephemeron (global_State *g, Table *h, int inv) {
static int traverseephemeron(global_State *g, Table *h, int inv) {
int marked = 0; /* true if an object is marked in this traversal */
int hasclears = 0; /* true if table has white keys */
int hasww = 0; /* true if table has entry "white-key -> white-value" */
@ -501,8 +511,7 @@ static int traverseephemeron (global_State *g, Table *h, int inv) {
hasclears = 1; /* table must be cleared */
if (valiswhite(gval(n))) /* value not marked yet? */
hasww = 1; /* white-white entry */
}
else if (valiswhite(gval(n))) { /* value not marked yet? */
} else if (valiswhite(gval(n))) { /* value not marked yet? */
marked = 1;
reallymarkobject(g, gcvalue(gval(n))); /* mark it now */
}
@ -520,7 +529,7 @@ static int traverseephemeron (global_State *g, Table *h, int inv) {
}
static void traversestrongtable (global_State *g, Table *h) {
static void traversestrongtable(global_State *g, Table *h) {
Node *n, *limit = gnodelast(h);
unsigned int i;
unsigned int asize = luaH_realasize(h);
@ -539,7 +548,7 @@ static void traversestrongtable (global_State *g, Table *h) {
}
static lu_mem traversetable (global_State *g, Table *h) {
static lu_mem traversetable(global_State *g, Table *h) {
const char *weakkey, *weakvalue;
const TValue *mode = gfasttm(g, h->metatable, TM_MODE);
TString *smode;
@ -555,14 +564,13 @@ static lu_mem traversetable (global_State *g, Table *h) {
traverseephemeron(g, h, 0);
else /* all weak */
linkgclist(h, g->allweak); /* nothing to traverse now */
}
else /* not weak */
} else /* not weak */
traversestrongtable(g, h);
return 1 + h->alimit + 2 * allocsizenode(h);
}
static int traverseudata (global_State *g, Udata *u) {
static int traverseudata(global_State *g, Udata *u) {
int i;
markobjectN(g, u->metatable); /* mark its metatable */
for (i = 0; i < u->nuvalue; i++)
@ -577,7 +585,7 @@ static int traverseudata (global_State *g, Udata *u) {
** arrays can be larger than needed; the extra slots are filled with
** NULL, so the use of 'markobjectN')
*/
static int traverseproto (global_State *g, Proto *f) {
static int traverseproto(global_State *g, Proto *f) {
int i;
markobjectN(g, f->source);
for (i = 0; i < f->sizek; i++) /* mark literals */
@ -592,7 +600,7 @@ static int traverseproto (global_State *g, Proto *f) {
}
static int traverseCclosure (global_State *g, CClosure *cl) {
static int traverseCclosure(global_State *g, CClosure *cl) {
int i;
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
markvalue(g, &cl->upvalue[i]);
@ -603,7 +611,7 @@ static int traverseCclosure (global_State *g, CClosure *cl) {
** Traverse a Lua closure, marking its prototype and its upvalues.
** (Both can be NULL while closure is being created.)
*/
static int traverseLclosure (global_State *g, LClosure *cl) {
static int traverseLclosure(global_State *g, LClosure *cl) {
int i;
markobjectN(g, cl->p); /* mark its prototype */
for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */
@ -626,7 +634,7 @@ static int traverseLclosure (global_State *g, LClosure *cl) {
** (which can only happen in generational mode) or if the traverse is in
** the propagate phase (which can only happen in incremental mode).
*/
static int traversethread (global_State *g, lua_State *th) {
static int traversethread(global_State *g, lua_State *th) {
UpVal *uv;
StkId o = th->stack.p;
if (isold(th) || g->gcstate == GCSpropagate)
@ -657,23 +665,31 @@ static int traversethread (global_State *g, lua_State *th) {
/*
** traverse one gray object, turning it to black.
*/
static lu_mem propagatemark (global_State *g) {
static lu_mem propagatemark(global_State *g) {
GCObject *o = g->gray;
nw2black(o);
g->gray = *getgclist(o); /* remove from 'gray' list */
switch (o->tt) {
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: return traversethread(g, gco2th(o));
default: lua_assert(0); return 0;
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:
return traversethread(g, gco2th(o));
default:
lua_assert(0);
return 0;
}
}
static lu_mem propagateall (global_State *g) {
static lu_mem propagateall(global_State *g) {
lu_mem tot = 0;
while (g->gray)
tot += propagatemark(g);
@ -688,7 +704,7 @@ static lu_mem propagateall (global_State *g) {
** convergence on chains in the same table.
**
*/
static void convergeephemerons (global_State *g) {
static void convergeephemerons(global_State *g) {
int changed;
int dir = 0;
do {
@ -722,7 +738,7 @@ static void convergeephemerons (global_State *g) {
/*
** clear entries with unmarked keys from all weaktables in list 'l'
*/
static void clearbykeys (global_State *g, GCObject *l) {
static void clearbykeys(global_State *g, GCObject *l) {
for (; l; l = gco2t(l)->gclist) {
Table *h = gco2t(l);
Node *limit = gnodelast(h);
@ -741,7 +757,7 @@ static void clearbykeys (global_State *g, GCObject *l) {
** clear entries with unmarked values from all weaktables in list 'l' up
** to element 'f'
*/
static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
static void clearbyvalues(global_State *g, GCObject *l, GCObject *f) {
for (; l != f; l = gco2t(l)->gclist) {
Table *h = gco2t(l);
Node *n, *limit = gnodelast(h);
@ -762,14 +778,14 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) {
}
static void freeupval (lua_State *L, UpVal *uv) {
static void freeupval(lua_State *L, UpVal *uv) {
if (upisopen(uv))
luaF_unlinkupval(uv);
luaM_free(L, uv);
}
static void freeobj (lua_State *L, GCObject *o) {
static void freeobj(lua_State *L, GCObject *o) {
switch (o->tt) {
case LUA_VPROTO:
luaF_freeproto(L, gco2p(o));
@ -809,7 +825,8 @@ static void freeobj (lua_State *L, GCObject *o) {
luaM_freemem(L, ts, sizelstring(ts->u.lnglen));
break;
}
default: lua_assert(0);
default:
lua_assert(0);
}
}
@ -821,7 +838,7 @@ static void freeobj (lua_State *L, GCObject *o) {
** collection cycle. Return where to continue the traversal or NULL if
** list is finished. ('*countout' gets the number of elements traversed.)
*/
static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
static GCObject **sweeplist(lua_State *L, GCObject **p, int countin,
int *countout) {
global_State *g = G(L);
int ow = otherwhite(g);
@ -833,8 +850,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
if (isdeadm(ow, marked)) { /* is 'curr' dead? */
*p = curr->next; /* remove 'curr' from list */
freeobj(L, curr); /* erase 'curr' */
}
else { /* change mark to 'white' */
} else { /* change mark to 'white' */
curr->marked = cast_byte((marked & ~maskgcbits) | white);
p = &curr->next; /* go to next element */
}
@ -848,7 +864,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin,
/*
** sweep a list until a live object (or end of list)
*/
static GCObject **sweeptolive (lua_State *L, GCObject **p) {
static GCObject **sweeptolive(lua_State *L, GCObject **p) {
GCObject **old = p;
do {
p = sweeplist(L, p, 1, NULL);
@ -868,7 +884,7 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) {
/*
** If possible, shrink string table.
*/
static void checkSizes (lua_State *L, global_State *g) {
static void checkSizes(lua_State *L, global_State *g) {
if (!g->gcemergency) {
if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */
l_mem olddebt = g->GCdebt;
@ -883,7 +899,7 @@ static void checkSizes (lua_State *L, global_State *g) {
** Get the next udata to be finalized from the 'tobefnz' list, and
** link it back into the 'allgc' list.
*/
static GCObject *udata2finalize (global_State *g) {
static GCObject *udata2finalize(global_State *g) {
GCObject *o = g->tobefnz; /* get first element */
lua_assert(tofinalize(o));
g->tobefnz = o->next; /* remove it from 'tobefnz' list */
@ -898,13 +914,13 @@ static GCObject *udata2finalize (global_State *g) {
}
static void dothecall (lua_State *L, void *ud) {
static void dothecall(lua_State *L, void *ud) {
UNUSED(ud);
luaD_callnoyield(L, L->top.p - 2, 0);
}
static void GCTM (lua_State *L) {
static void GCTM(lua_State *L) {
global_State *g = G(L);
const TValue *tm;
TValue v;
@ -935,7 +951,7 @@ static void GCTM (lua_State *L) {
/*
** Call a few finalizers
*/
static int runafewfinalizers (lua_State *L, int n) {
static int runafewfinalizers(lua_State *L, int n) {
global_State *g = G(L);
int i;
for (i = 0; i < n && g->tobefnz; i++)
@ -947,7 +963,7 @@ static int runafewfinalizers (lua_State *L, int n) {
/*
** call all pending finalizers
*/
static void callallpendingfinalizers (lua_State *L) {
static void callallpendingfinalizers(lua_State *L) {
global_State *g = G(L);
while (g->tobefnz)
GCTM(L);
@ -957,7 +973,7 @@ static void callallpendingfinalizers (lua_State *L) {
/*
** find last 'next' field in list 'p' list (to add elements in its end)
*/
static GCObject **findlast (GCObject **p) {
static GCObject **findlast(GCObject **p) {
while (*p != NULL)
p = &(*p)->next;
return p;
@ -971,7 +987,7 @@ static GCObject **findlast (GCObject **p) {
** don't need to be traversed. In incremental mode, 'finobjold1' is NULL,
** so the whole list is traversed.)
*/
static void separatetobefnz (global_State *g, int all) {
static void separatetobefnz(global_State *g, int all) {
GCObject *curr;
GCObject **p = &g->finobj;
GCObject **lastnext = findlast(&g->tobefnz);
@ -994,7 +1010,7 @@ static void separatetobefnz (global_State *g, int all) {
/*
** If pointer 'p' points to 'o', move it to the next element.
*/
static void checkpointer (GCObject **p, GCObject *o) {
static void checkpointer(GCObject **p, GCObject *o) {
if (o == *p)
*p = o->next;
}
@ -1004,7 +1020,7 @@ static void checkpointer (GCObject **p, GCObject *o) {
** Correct pointers to objects inside 'allgc' list when
** object 'o' is being removed from the list.
*/
static void correctpointers (global_State *g, GCObject *o) {
static void correctpointers(global_State *g, GCObject *o) {
checkpointer(&g->survival, o);
checkpointer(&g->old1, o);
checkpointer(&g->reallyold, o);
@ -1016,7 +1032,7 @@ static void correctpointers (global_State *g, GCObject *o) {
** if object 'o' has a finalizer, remove it from 'allgc' list (must
** search the list to find it) and link it in 'finobj' list.
*/
void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt) {
global_State *g = G(L);
if (tofinalize(o) || /* obj. is already marked... */
gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */
@ -1028,8 +1044,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
makewhite(g, o); /* "sweep" object 'o' */
if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
}
else
} else
correctpointers(g, o);
/* search for pointer pointing to 'o' */
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
@ -1056,7 +1071,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero,
** because Lua cannot even start with less than PAUSEADJ bytes).
*/
static void setpause (global_State *g) {
static void setpause(global_State *g) {
l_mem threshold, debt;
int pause = getgcparam(g->gcpause);
l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */
@ -1076,7 +1091,7 @@ static void setpause (global_State *g) {
** are now old---must be in a gray list. Everything else is not in a
** gray list. Open upvalues are also kept gray.
*/
static void sweep2old (lua_State *L, GCObject **p) {
static void sweep2old(lua_State *L, GCObject **p) {
GCObject *curr;
global_State *g = G(L);
while ((curr = *p) != NULL) {
@ -1084,14 +1099,12 @@ static void sweep2old (lua_State *L, GCObject **p) {
lua_assert(isdead(g, curr));
*p = curr->next; /* remove 'curr' from list */
freeobj(L, curr); /* erase 'curr' */
}
else { /* all surviving objects become old */
} else { /* all surviving objects become old */
setage(curr, G_OLD);
if (curr->tt == LUA_VTHREAD) { /* threads must be watched */
lua_State *th = gco2th(curr);
linkgclist(th, g->grayagain); /* insert into 'grayagain' list */
}
else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr)))
} else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr)))
set2gray(curr); /* open upvalues are always gray */
else /* everything else is black */
nw2black(curr);
@ -1112,7 +1125,7 @@ static void sweep2old (lua_State *L, GCObject **p) {
** here. They will all be advanced in 'correctgraylist'. That function
** will also remove objects turned white here from any gray list.
*/
static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
static GCObject **sweepgen(lua_State *L, global_State *g, GCObject **p,
GCObject *limit, GCObject **pfirstold1) {
static const lu_byte nextage[] = {
G_SURVIVAL, /* from G_NEW */
@ -1130,13 +1143,11 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
lua_assert(!isold(curr) && isdead(g, curr));
*p = curr->next; /* remove 'curr' from list */
freeobj(L, curr); /* erase 'curr' */
}
else { /* correct mark and age */
} else { /* correct mark and age */
if (getage(curr) == G_NEW) { /* new objects go back to white */
int marked = curr->marked & ~maskgcbits; /* erase GC bits */
curr->marked = cast_byte(marked | G_SURVIVAL | white);
}
else { /* all other objects will be old, and so keep their color */
} else { /* all other objects will be old, and so keep their color */
setage(curr, nextage[getage(curr)]);
if (getage(curr) == G_OLD1 && *pfirstold1 == NULL)
*pfirstold1 = curr; /* first OLD1 object in the list */
@ -1153,7 +1164,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p,
** age. In incremental mode, all objects are 'new' all the time,
** except for fixed strings (which are always old).
*/
static void whitelist (global_State *g, GCObject *p) {
static void whitelist(global_State *g, GCObject *p) {
int white = luaC_white(g);
for (; p != NULL; p = p->next)
p->marked = cast_byte((p->marked & ~maskgcbits) | white);
@ -1169,7 +1180,7 @@ static void whitelist (global_State *g, GCObject *p) {
** Non-white threads also remain on the list; 'TOUCHED2' objects become
** regular old; they and anything else are removed from the list.
*/
static GCObject **correctgraylist (GCObject **p) {
static GCObject **correctgraylist(GCObject **p) {
GCObject *curr;
while ((curr = *p) != NULL) {
GCObject **next = getgclist(curr);
@ -1180,20 +1191,22 @@ static GCObject **correctgraylist (GCObject **p) {
nw2black(curr); /* make it black, for next barrier */
changeage(curr, G_TOUCHED1, G_TOUCHED2);
goto remain; /* keep it in the list and go to next element */
}
else if (curr->tt == LUA_VTHREAD) {
} else if (curr->tt == LUA_VTHREAD) {
lua_assert(isgray(curr));
goto remain; /* keep non-white threads on the list */
}
else { /* everything else is removed */
} else { /* everything else is removed */
lua_assert(isold(curr)); /* young objects should be white here */
if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */
changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */
nw2black(curr); /* make object black (to be removed) */
goto remove;
}
remove: *p = *next; continue;
remain: p = next; continue;
remove:
*p = *next;
continue;
remain:
p = next;
continue;
}
return p;
}
@ -1202,13 +1215,16 @@ static GCObject **correctgraylist (GCObject **p) {
/*
** Correct all gray lists, coalescing them into 'grayagain'.
*/
static void correctgraylists (global_State *g) {
static void correctgraylists(global_State *g) {
GCObject **list = correctgraylist(&g->grayagain);
*list = g->weak; g->weak = NULL;
*list = g->weak;
g->weak = NULL;
list = correctgraylist(list);
*list = g->allweak; g->allweak = NULL;
*list = g->allweak;
g->allweak = NULL;
list = correctgraylist(list);
*list = g->ephemeron; g->ephemeron = NULL;
*list = g->ephemeron;
g->ephemeron = NULL;
correctgraylist(list);
}
@ -1218,7 +1234,7 @@ static void correctgraylists (global_State *g) {
** Gray objects are already in some gray list, and so will be visited
** in the atomic step.
*/
static void markold (global_State *g, GCObject *from, GCObject *to) {
static void markold(global_State *g, GCObject *from, GCObject *to) {
GCObject *p;
for (p = from; p != to; p = p->next) {
if (getage(p) == G_OLD1) {
@ -1234,7 +1250,7 @@ static void markold (global_State *g, GCObject *from, GCObject *to) {
/*
** Finish a young-generation collection.
*/
static void finishgencycle (lua_State *L, global_State *g) {
static void finishgencycle(lua_State *L, global_State *g) {
correctgraylists(g);
checkSizes(L, g);
g->gcstate = GCSpropagate; /* skip restart */
@ -1248,7 +1264,7 @@ static void finishgencycle (lua_State *L, global_State *g) {
** atomic step. Then, sweep all lists and advance pointers. Finally,
** finish the collection.
*/
static void youngcollection (lua_State *L, global_State *g) {
static void youngcollection(lua_State *L, global_State *g) {
GCObject **psurvival; /* to point to first non-dead survival object */
GCObject *dummy; /* dummy out parameter to 'sweepgen' */
lua_assert(g->gcstate == GCSpropagate);
@ -1289,7 +1305,7 @@ static void youngcollection (lua_State *L, global_State *g) {
** surviving objects to old. Threads go back to 'grayagain'; everything
** else is turned black (not in any gray list).
*/
static void atomic2gen (lua_State *L, global_State *g) {
static void atomic2gen(lua_State *L, global_State *g) {
cleargraylists(g);
/* sweep all elements making them old */
g->gcstate = GCSswpallgc;
@ -1315,7 +1331,7 @@ static void atomic2gen (lua_State *L, global_State *g) {
** Set debt for the next minor collection, which will happen when
** memory grows 'genminormul'%.
*/
static void setminordebt (global_State *g) {
static void setminordebt(global_State *g) {
luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul));
}
@ -1326,7 +1342,7 @@ static void setminordebt (global_State *g) {
** are cleared. Then, turn all objects into old and finishes the
** collection.
*/
static lu_mem entergen (lua_State *L, global_State *g) {
static lu_mem entergen(lua_State *L, global_State *g) {
lu_mem numobjs;
luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */
luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */
@ -1342,7 +1358,7 @@ static lu_mem entergen (lua_State *L, global_State *g) {
** intermediate lists point to NULL (to avoid invalid pointers),
** and go to the pause state.
*/
static void enterinc (global_State *g) {
static void enterinc(global_State *g) {
whitelist(g, g->allgc);
g->reallyold = g->old1 = g->survival = NULL;
whitelist(g, g->finobj);
@ -1357,7 +1373,7 @@ static void enterinc (global_State *g) {
/*
** Change collector mode to 'newmode'.
*/
void luaC_changemode (lua_State *L, int newmode) {
void luaC_changemode(lua_State *L, int newmode) {
global_State *g = G(L);
if (newmode != g->gckind) {
if (newmode == KGC_GEN) /* entering generational mode? */
@ -1372,7 +1388,7 @@ void luaC_changemode (lua_State *L, int newmode) {
/*
** Does a full collection in generational mode.
*/
static lu_mem fullgen (lua_State *L, global_State *g) {
static lu_mem fullgen(lua_State *L, global_State *g) {
enterinc(g);
return entergen(L, g);
}
@ -1399,7 +1415,7 @@ static lu_mem fullgen (lua_State *L, global_State *g) {
** field 'g->lastatomic' keeps this count from the last collection.
** ('g->lastatomic != 0' also means that the last collection was bad.)
*/
static void stepgenfull (lua_State *L, global_State *g) {
static void stepgenfull(lua_State *L, global_State *g) {
lu_mem newatomic; /* count of traversed objects */
lu_mem lastatomic = g->lastatomic; /* count from last collection */
if (g->gckind == KGC_GEN) /* still in generational mode? */
@ -1409,8 +1425,7 @@ static void stepgenfull (lua_State *L, global_State *g) {
if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */
atomic2gen(L, g); /* return to generational mode */
setminordebt(g);
}
else { /* another bad collection; stay in incremental mode */
} else { /* another bad collection; stay in incremental mode */
g->GCestimate = gettotalbytes(g); /* first estimate */
entersweep(L);
luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */
@ -1439,7 +1454,7 @@ static void stepgenfull (lua_State *L, global_State *g) {
** 'GCdebt <= 0' means an explicit call to GC step with "size" zero;
** in that case, do a minor collection.
*/
static void genstep (lua_State *L, global_State *g) {
static void genstep(lua_State *L, global_State *g) {
if (g->lastatomic != 0) /* last collection was a bad one? */
stepgenfull(L, g); /* do a full step */
else {
@ -1451,13 +1466,11 @@ static void genstep (lua_State *L, global_State *g) {
/* collected at least half of memory growth since last major
collection; keep doing minor collections. */
lua_assert(g->lastatomic == 0);
}
else { /* bad collection */
} else { /* bad collection */
g->lastatomic = numobjs; /* signal that last collection was bad */
setpause(g); /* do a long wait for next (major) collection */
}
}
else { /* regular case; do a minor collection */
} else { /* regular case; do a minor collection */
youngcollection(L, g);
setminordebt(g);
g->GCestimate = majorbase; /* preserve base value */
@ -1483,7 +1496,7 @@ static void genstep (lua_State *L, global_State *g) {
** not need to skip objects created between "now" and the start of the
** real sweep.
*/
static void entersweep (lua_State *L) {
static void entersweep(lua_State *L) {
global_State *g = G(L);
g->gcstate = GCSswpallgc;
lua_assert(g->sweepgc == NULL);
@ -1495,7 +1508,7 @@ static void entersweep (lua_State *L) {
** Delete all objects in list 'p' until (but not including) object
** 'limit'.
*/
static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
static void deletelist(lua_State *L, GCObject *p, GCObject *limit) {
while (p != limit) {
GCObject *next = p->next;
freeobj(L, p);
@ -1508,7 +1521,7 @@ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) {
** Call all finalizers of the objects in the given Lua state, and
** then free all objects, except for the main thread.
*/
void luaC_freeallobjects (lua_State *L) {
void luaC_freeallobjects(lua_State *L) {
global_State *g = G(L);
g->gcstp = GCSTPCLS; /* no extra finalizers after here */
luaC_changemode(L, KGC_INC);
@ -1522,7 +1535,7 @@ void luaC_freeallobjects (lua_State *L) {
}
static lu_mem atomic (lua_State *L) {
static lu_mem atomic(lua_State *L) {
global_State *g = G(L);
lu_mem work = 0;
GCObject *origweak, *origall;
@ -1546,7 +1559,8 @@ static lu_mem atomic (lua_State *L) {
/* Clear values from weak tables, before checking finalizers */
clearbyvalues(g, g->weak, NULL);
clearbyvalues(g, g->allweak, NULL);
origweak = g->weak; origall = g->allweak;
origweak = g->weak;
origall = g->allweak;
separatetobefnz(g, 0); /* separate objects to be finalized */
work += markbeingfnz(g); /* mark objects that will be finalized */
work += propagateall(g); /* remark, to propagate 'resurrection' */
@ -1565,7 +1579,7 @@ static lu_mem atomic (lua_State *L) {
}
static int sweepstep (lua_State *L, global_State *g,
static int sweepstep(lua_State *L, global_State *g,
int nextstate, GCObject **nextlist) {
if (g->sweepgc) {
l_mem olddebt = g->GCdebt;
@ -1573,8 +1587,7 @@ static int sweepstep (lua_State *L, global_State *g,
g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count);
g->GCestimate += g->GCdebt - olddebt; /* update estimate */
return count;
}
else { /* enter next state */
} else { /* enter next state */
g->gcstate = nextstate;
g->sweepgc = nextlist;
return 0; /* no work done */
@ -1582,7 +1595,7 @@ static int sweepstep (lua_State *L, global_State *g,
}
static lu_mem singlestep (lua_State *L) {
static lu_mem singlestep(lua_State *L) {
global_State *g = G(L);
lu_mem work;
lua_assert(!g->gcstopem); /* collector is not reentrant */
@ -1598,8 +1611,7 @@ static lu_mem singlestep (lua_State *L) {
if (g->gray == NULL) { /* no more gray objects? */
g->gcstate = GCSenteratomic; /* finish propagate phase */
work = 0;
}
else
} else
work = propagatemark(g); /* traverse one gray object */
break;
}
@ -1631,14 +1643,15 @@ static lu_mem singlestep (lua_State *L) {
if (g->tobefnz && !g->gcemergency) {
g->gcstopem = 0; /* ok collections during finalizers */
work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST;
}
else { /* emergency mode or no more finalizers */
} else { /* emergency mode or no more finalizers */
g->gcstate = GCSpause; /* finish collection */
work = 0;
}
break;
}
default: lua_assert(0); return 0;
default:
lua_assert(0);
return 0;
}
g->gcstopem = 0;
return work;
@ -1649,7 +1662,7 @@ static lu_mem singlestep (lua_State *L) {
** advances the garbage collector until it reaches a state allowed
** by 'statemask'
*/
void luaC_runtilstate (lua_State *L, int statesmask) {
void luaC_runtilstate(lua_State *L, int statesmask) {
global_State *g = G(L);
while (!testbit(statesmask, g->gcstate))
singlestep(L);
@ -1664,7 +1677,7 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
** finishing a cycle (pause state). Finally, it sets the debt that
** controls when next step will be performed.
*/
static void incstep (lua_State *L, global_State *g) {
static void incstep(lua_State *L, global_State *g) {
int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */
l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
@ -1687,12 +1700,12 @@ static void incstep (lua_State *L, global_State *g) {
** not running, set a reasonable debt to avoid it being called at
** every single check.)
*/
void luaC_step (lua_State *L) {
void luaC_step(lua_State *L) {
global_State *g = G(L);
if (!gcrunning(g)) /* not running? */
luaE_setdebt(g, -2000);
else {
if(isdecGCmodegen(g))
if (isdecGCmodegen(g))
genstep(L, g);
else
incstep(L, g);
@ -1707,7 +1720,7 @@ void luaC_step (lua_State *L) {
** to sweep all objects to turn them back to white (as white has not
** changed, nothing will be collected).
*/
static void fullinc (lua_State *L, global_State *g) {
static void fullinc(lua_State *L, global_State *g) {
if (keepinvariant(g)) /* black objects? */
entersweep(L); /* sweep everything to turn them back to white */
/* finish any pending sweep phase to start a new cycle */
@ -1727,7 +1740,7 @@ static void fullinc (lua_State *L, global_State *g) {
** some operations which could change the interpreter state in some
** unexpected ways (running finalizers and shrinking some structures).
*/
void luaC_fullgc (lua_State *L, int isemergency) {
void luaC_fullgc(lua_State *L, int isemergency) {
global_State *g = G(L);
lua_assert(!g->gcemergency);
g->gcemergency = isemergency; /* set flag */

View file

@ -185,18 +185,18 @@
#define luaC_barrierback(L,p,v) ( \
iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
LUAI_FUNC void luaC_step (lua_State *L);
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
LUAI_FUNC void luaC_fix(lua_State *L, GCObject *o);
LUAI_FUNC void luaC_freeallobjects(lua_State *L);
LUAI_FUNC void luaC_step(lua_State *L);
LUAI_FUNC void luaC_runtilstate(lua_State *L, int statesmask);
LUAI_FUNC void luaC_fullgc(lua_State *L, int isemergency);
LUAI_FUNC GCObject *luaC_newobj(lua_State *L, int tt, size_t sz);
LUAI_FUNC GCObject *luaC_newobjdt(lua_State *L, int tt, size_t sz,
size_t offset);
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
LUAI_FUNC void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v);
LUAI_FUNC void luaC_barrierback_(lua_State *L, GCObject *o);
LUAI_FUNC void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt);
LUAI_FUNC void luaC_changemode(lua_State *L, int newmode);
#endif

View file

@ -54,7 +54,7 @@ static const luaL_Reg loadedlibs[] = {
};
LUALIB_API void luaL_openlibs (lua_State *L) {
LUALIB_API void luaL_openlibs(lua_State *L) {
const luaL_Reg *lib;
/* "require" functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {

View file

@ -37,7 +37,7 @@
#endif
/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
static int l_checkmode (const char *mode) {
static int l_checkmode(const char *mode) {
return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&
(*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */
(strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */
@ -160,7 +160,7 @@ typedef luaL_Stream LStream;
#define isclosed(p) ((p)->closef == NULL)
static int io_type (lua_State *L) {
static int io_type(lua_State *L) {
LStream *p;
luaL_checkany(L, 1);
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
@ -174,7 +174,7 @@ static int io_type (lua_State *L) {
}
static int f_tostring (lua_State *L) {
static int f_tostring(lua_State *L) {
LStream *p = tolstream(L);
if (isclosed(p))
lua_pushliteral(L, "file (closed)");
@ -184,7 +184,7 @@ static int f_tostring (lua_State *L) {
}
static FILE *tofile (lua_State *L) {
static FILE *tofile(lua_State *L) {
LStream *p = tolstream(L);
if (l_unlikely(isclosed(p)))
luaL_error(L, "attempt to use a closed file");
@ -198,7 +198,7 @@ static FILE *tofile (lua_State *L) {
** before opening the actual file; so, if there is a memory error, the
** handle is in a consistent state.
*/
static LStream *newprefile (lua_State *L) {
static LStream *newprefile(lua_State *L) {
LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0);
p->closef = NULL; /* mark file handle as 'closed' */
luaL_setmetatable(L, LUA_FILEHANDLE);
@ -211,7 +211,7 @@ static LStream *newprefile (lua_State *L) {
** a bug in some versions of the Clang compiler (e.g., clang 3.0 for
** 32 bits).
*/
static int aux_close (lua_State *L) {
static int aux_close(lua_State *L) {
LStream *p = tolstream(L);
volatile lua_CFunction cf = p->closef;
p->closef = NULL; /* mark stream as closed */
@ -219,20 +219,20 @@ static int aux_close (lua_State *L) {
}
static int f_close (lua_State *L) {
static int f_close(lua_State *L) {
tofile(L); /* make sure argument is an open stream */
return aux_close(L);
}
static int io_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 default output */
return f_close(L);
}
static int f_gc (lua_State *L) {
static int f_gc(lua_State *L) {
LStream *p = tolstream(L);
if (!isclosed(p) && p->f != NULL)
aux_close(L); /* ignore closed and incompletely open files */
@ -243,14 +243,14 @@ static int f_gc (lua_State *L) {
/*
** function to close regular files
*/
static int io_fclose (lua_State *L) {
static int io_fclose(lua_State *L) {
LStream *p = tolstream(L);
errno = 0;
return luaL_fileresult(L, (fclose(p->f) == 0), NULL);
}
static LStream *newfile (lua_State *L) {
static LStream *newfile(lua_State *L) {
LStream *p = newprefile(L);
p->f = NULL;
p->closef = &io_fclose;
@ -258,7 +258,7 @@ static LStream *newfile (lua_State *L) {
}
static void opencheck (lua_State *L, const char *fname, const char *mode) {
static void opencheck(lua_State *L, const char *fname, const char *mode) {
LStream *p = newfile(L);
p->f = fopen(fname, mode);
if (l_unlikely(p->f == NULL))
@ -266,7 +266,7 @@ static void opencheck (lua_State *L, const char *fname, const char *mode) {
}
static int io_open (lua_State *L) {
static int io_open(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
LStream *p = newfile(L);
@ -281,14 +281,14 @@ static int io_open (lua_State *L) {
/*
** function to close 'popen' files
*/
static int io_pclose (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));
}
static int io_popen (lua_State *L) {
static int io_popen(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
LStream *p = newprefile(L);
@ -300,7 +300,7 @@ static int io_popen (lua_State *L) {
}
static int io_tmpfile (lua_State *L) {
static int io_tmpfile(lua_State *L) {
LStream *p = newfile(L);
errno = 0;
p->f = tmpfile();
@ -308,7 +308,7 @@ static int io_tmpfile (lua_State *L) {
}
static FILE *getiofile (lua_State *L, const char *findex) {
static FILE *getiofile(lua_State *L, const char *findex) {
LStream *p;
lua_getfield(L, LUA_REGISTRYINDEX, findex);
p = (LStream *)lua_touserdata(L, -1);
@ -318,7 +318,7 @@ static FILE *getiofile (lua_State *L, const char *findex) {
}
static int g_iofile (lua_State *L, const char *f, const char *mode) {
static int g_iofile(lua_State *L, const char *f, const char *mode) {
if (!lua_isnoneornil(L, 1)) {
const char *filename = lua_tostring(L, 1);
if (filename)
@ -335,17 +335,17 @@ static int g_iofile (lua_State *L, const char *f, const char *mode) {
}
static int io_input (lua_State *L) {
static int io_input(lua_State *L) {
return g_iofile(L, IO_INPUT, "r");
}
static int io_output (lua_State *L) {
static int io_output(lua_State *L) {
return g_iofile(L, IO_OUTPUT, "w");
}
static int io_readline (lua_State *L);
static int io_readline(lua_State *L);
/*
@ -363,7 +363,7 @@ static int io_readline (lua_State *L);
** 3) a boolean, true iff file has to be closed when finished ('toclose')
** *) a variable number of format arguments (rest of the stack)
*/
static void aux_lines (lua_State *L, int toclose) {
static void aux_lines(lua_State *L, int toclose) {
int n = lua_gettop(L) - 1; /* number of arguments to read */
luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
lua_pushvalue(L, 1); /* file */
@ -374,7 +374,7 @@ static void aux_lines (lua_State *L, int toclose) {
}
static int f_lines (lua_State *L) {
static int f_lines(lua_State *L) {
tofile(L); /* check that it's a valid file handle */
aux_lines(L, 0);
return 1;
@ -386,7 +386,7 @@ static int f_lines (lua_State *L) {
** closed, also returns the file itself as a second result (to be
** closed as the state at the exit of a generic for).
*/
static int io_lines (lua_State *L) {
static int io_lines(lua_State *L) {
int toclose;
if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
if (lua_isnil(L, 1)) { /* no file name? */
@ -394,8 +394,7 @@ static int io_lines (lua_State *L) {
lua_replace(L, 1); /* put it at index 1 */
tofile(L); /* check that it's a valid file handle */
toclose = 0; /* do not close it after iteration */
}
else { /* open a new file */
} else { /* open a new file */
const char *filename = luaL_checkstring(L, 1);
opencheck(L, filename, "r");
lua_replace(L, 1); /* put file at index 1 */
@ -407,8 +406,7 @@ static int io_lines (lua_State *L) {
lua_pushnil(L); /* control */
lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */
return 4;
}
else
} else
return 1;
}
@ -438,12 +436,11 @@ typedef struct {
/*
** Add current char to buffer (if not out of space) and read next one
*/
static int nextc (RN *rn) {
static int nextc(RN *rn) {
if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */
rn->buff[0] = '\0'; /* invalidate result */
return 0; /* fail */
}
else {
} else {
rn->buff[rn->n++] = rn->c; /* save current char */
rn->c = l_getc(rn->f); /* read next one */
return 1;
@ -454,7 +451,7 @@ static int nextc (RN *rn) {
/*
** Accept current char if it is in 'set' (of size 2)
*/
static int test2 (RN *rn, const char *set) {
static int test2(RN *rn, const char *set) {
if (rn->c == set[0] || rn->c == set[1])
return nextc(rn);
else return 0;
@ -464,7 +461,7 @@ static int test2 (RN *rn, const char *set) {
/*
** Read a sequence of (hex)digits
*/
static int readdigits (RN *rn, int hex) {
static int readdigits(RN *rn, int hex) {
int count = 0;
while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
count++;
@ -477,16 +474,18 @@ static int readdigits (RN *rn, int hex) {
** Then it calls 'lua_stringtonumber' to check whether the format is
** correct and to convert it to a Lua number.
*/
static int read_number (lua_State *L, FILE *f) {
static int read_number(lua_State *L, FILE *f) {
RN rn;
int count = 0;
int hex = 0;
char decp[2];
rn.f = f; rn.n = 0;
rn.f = f;
rn.n = 0;
decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
decp[1] = '.'; /* always accept a dot */
l_lockfile(rn.f);
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
do { rn.c = l_getc(rn.f); }
while (isspace(rn.c)); /* skip spaces */
test2(&rn, "-+"); /* optional sign */
if (test2(&rn, "00")) {
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
@ -511,7 +510,7 @@ static int read_number (lua_State *L, FILE *f) {
}
static int test_eof (lua_State *L, FILE *f) {
static int test_eof(lua_State *L, FILE *f) {
int c = getc(f);
ungetc(c, f); /* no-op when c == EOF */
lua_pushliteral(L, "");
@ -519,7 +518,7 @@ static int test_eof (lua_State *L, FILE *f) {
}
static int read_line (lua_State *L, FILE *f, int chop) {
static int read_line(lua_State *L, FILE *f, int chop) {
luaL_Buffer b;
int c;
luaL_buffinit(L, &b);
@ -540,7 +539,7 @@ static int read_line (lua_State *L, FILE *f, int chop) {
}
static void read_all (lua_State *L, FILE *f) {
static void read_all(lua_State *L, FILE *f) {
size_t nr;
luaL_Buffer b;
luaL_buffinit(L, &b);
@ -553,7 +552,7 @@ static void read_all (lua_State *L, FILE *f) {
}
static int read_chars (lua_State *L, FILE *f, size_t n) {
static int read_chars(lua_State *L, FILE *f, size_t n) {
size_t nr; /* number of chars actually read */
char *p;
luaL_Buffer b;
@ -566,7 +565,7 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
}
static int g_read (lua_State *L, FILE *f, int first) {
static int g_read(lua_State *L, FILE *f, int first) {
int nargs = lua_gettop(L) - 1;
int n, success;
clearerr(f);
@ -574,17 +573,15 @@ static int g_read (lua_State *L, FILE *f, int first) {
if (nargs == 0) { /* no arguments? */
success = read_line(L, f, 1);
n = first + 1; /* to return 1 result */
}
else {
} else {
/* ensure stack space for all results and for auxlib's buffer */
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
luaL_checkstack(L, nargs + LUA_MINSTACK, "too many arguments");
success = 1;
for (n = first; nargs-- && success; n++) {
if (lua_type(L, n) == LUA_TNUMBER) {
size_t l = (size_t)luaL_checkinteger(L, n);
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
}
else {
} else {
const char *p = luaL_checkstring(L, n);
if (*p == '*') p++; /* skip optional '*' (for compatibility) */
switch (*p) {
@ -617,12 +614,12 @@ static int g_read (lua_State *L, FILE *f, int first) {
}
static int io_read (lua_State *L) {
static int io_read(lua_State *L) {
return g_read(L, getiofile(L, IO_INPUT), 1);
}
static int f_read (lua_State *L) {
static int f_read(lua_State *L) {
return g_read(L, tofile(L), 2);
}
@ -630,13 +627,13 @@ static int f_read (lua_State *L) {
/*
** Iteration function for 'lines'.
*/
static int io_readline (lua_State *L) {
static int io_readline(lua_State *L) {
LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
int i;
int n = (int)lua_tointeger(L, lua_upvalueindex(2));
if (isclosed(p)) /* file is already closed? */
return luaL_error(L, "file is already closed");
lua_settop(L , 1);
lua_settop(L, 1);
luaL_checkstack(L, n, "too many arguments");
for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
lua_pushvalue(L, lua_upvalueindex(3 + i));
@ -661,7 +658,7 @@ static int io_readline (lua_State *L) {
/* }====================================================== */
static int g_write (lua_State *L, FILE *f, int arg) {
static int g_write(lua_State *L, FILE *f, int arg) {
int nargs = lua_gettop(L) - arg;
int status = 1;
errno = 0;
@ -674,8 +671,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
: fprintf(f, LUA_NUMBER_FMT,
(LUAI_UACNUMBER)lua_tonumber(L, arg));
status = status && (len > 0);
}
else {
} else {
size_t l;
const char *s = luaL_checklstring(L, arg, &l);
status = status && (fwrite(s, sizeof(char), l, f) == l);
@ -688,19 +684,19 @@ static int g_write (lua_State *L, FILE *f, int arg) {
}
static int io_write (lua_State *L) {
static int io_write(lua_State *L) {
return g_write(L, getiofile(L, IO_OUTPUT), 1);
}
static int f_write (lua_State *L) {
static int f_write(lua_State *L) {
FILE *f = tofile(L);
lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
return g_write(L, f, 2);
}
static int f_seek (lua_State *L) {
static int f_seek(lua_State *L) {
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
static const char *const modenames[] = {"set", "cur", "end", NULL};
FILE *f = tofile(L);
@ -720,7 +716,7 @@ static int f_seek (lua_State *L) {
}
static int f_setvbuf (lua_State *L) {
static int f_setvbuf(lua_State *L) {
static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
static const char *const modenames[] = {"no", "full", "line", NULL};
FILE *f = tofile(L);
@ -734,14 +730,14 @@ static int f_setvbuf (lua_State *L) {
static int io_flush (lua_State *L) {
static int io_flush(lua_State *L) {
FILE *f = getiofile(L, IO_OUTPUT);
errno = 0;
return luaL_fileresult(L, fflush(f) == 0, NULL);
}
static int f_flush (lua_State *L) {
static int f_flush(lua_State *L) {
FILE *f = tofile(L);
errno = 0;
return luaL_fileresult(L, fflush(f) == 0, NULL);
@ -794,7 +790,7 @@ static const luaL_Reg metameth[] = {
};
static void createmeta (lua_State *L) {
static void createmeta(lua_State *L) {
luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
luaL_newlibtable(L, meth); /* create method table */
@ -807,7 +803,7 @@ static void createmeta (lua_State *L) {
/*
** function to (not) close the standard files stdin, stdout, and stderr
*/
static int io_noclose (lua_State *L) {
static int io_noclose(lua_State *L) {
LStream *p = tolstream(L);
p->closef = &io_noclose; /* keep file opened */
luaL_pushfail(L);
@ -816,7 +812,7 @@ static int io_noclose (lua_State *L) {
}
static void createstdfile (lua_State *L, FILE *f, const char *k,
static void createstdfile(lua_State *L, FILE *f, const char *k,
const char *fname) {
LStream *p = newprefile(L);
p->f = f;
@ -829,7 +825,7 @@ static void createstdfile (lua_State *L, FILE *f, const char *k,
}
LUAMOD_API int luaopen_io (lua_State *L) {
LUAMOD_API int luaopen_io(lua_State *L) {
luaL_newlib(L, iolib); /* new module */
createmeta(L);
/* create (and set) default files */

View file

@ -19,94 +19,94 @@
static const void *const disptab[NUM_OPCODES] = {
#if 0
** you can update the following list with this command:
**
** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h
**
**you can update the following list with this command:
**
**sed - n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h
**
#endif
&&L_OP_MOVE,
&&L_OP_LOADI,
&&L_OP_LOADF,
&&L_OP_LOADK,
&&L_OP_LOADKX,
&&L_OP_LOADFALSE,
&&L_OP_LFALSESKIP,
&&L_OP_LOADTRUE,
&&L_OP_LOADNIL,
&&L_OP_GETUPVAL,
&&L_OP_SETUPVAL,
&&L_OP_GETTABUP,
&&L_OP_GETTABLE,
&&L_OP_GETI,
&&L_OP_GETFIELD,
&&L_OP_SETTABUP,
&&L_OP_SETTABLE,
&&L_OP_SETI,
&&L_OP_SETFIELD,
&&L_OP_NEWTABLE,
&&L_OP_SELF,
&&L_OP_ADDI,
&&L_OP_ADDK,
&&L_OP_SUBK,
&&L_OP_MULK,
&&L_OP_MODK,
&&L_OP_POWK,
&&L_OP_DIVK,
&&L_OP_IDIVK,
&&L_OP_BANDK,
&&L_OP_BORK,
&&L_OP_BXORK,
&&L_OP_SHRI,
&&L_OP_SHLI,
&&L_OP_ADD,
&&L_OP_SUB,
&&L_OP_MUL,
&&L_OP_MOD,
&&L_OP_POW,
&&L_OP_DIV,
&&L_OP_IDIV,
&&L_OP_BAND,
&&L_OP_BOR,
&&L_OP_BXOR,
&&L_OP_SHL,
&&L_OP_SHR,
&&L_OP_MMBIN,
&&L_OP_MMBINI,
&&L_OP_MMBINK,
&&L_OP_UNM,
&&L_OP_BNOT,
&&L_OP_NOT,
&&L_OP_LEN,
&&L_OP_CONCAT,
&&L_OP_CLOSE,
&&L_OP_TBC,
&&L_OP_JMP,
&&L_OP_EQ,
&&L_OP_LT,
&&L_OP_LE,
&&L_OP_EQK,
&&L_OP_EQI,
&&L_OP_LTI,
&&L_OP_LEI,
&&L_OP_GTI,
&&L_OP_GEI,
&&L_OP_TEST,
&&L_OP_TESTSET,
&&L_OP_CALL,
&&L_OP_TAILCALL,
&&L_OP_RETURN,
&&L_OP_RETURN0,
&&L_OP_RETURN1,
&&L_OP_FORLOOP,
&&L_OP_FORPREP,
&&L_OP_TFORPREP,
&&L_OP_TFORCALL,
&&L_OP_TFORLOOP,
&&L_OP_SETLIST,
&&L_OP_CLOSURE,
&&L_OP_VARARG,
&&L_OP_VARARGPREP,
&&L_OP_EXTRAARG
&&L_OP_MOVE,
&&L_OP_LOADI,
&&L_OP_LOADF,
&&L_OP_LOADK,
&&L_OP_LOADKX,
&&L_OP_LOADFALSE,
&&L_OP_LFALSESKIP,
&&L_OP_LOADTRUE,
&&L_OP_LOADNIL,
&&L_OP_GETUPVAL,
&&L_OP_SETUPVAL,
&&L_OP_GETTABUP,
&&L_OP_GETTABLE,
&&L_OP_GETI,
&&L_OP_GETFIELD,
&&L_OP_SETTABUP,
&&L_OP_SETTABLE,
&&L_OP_SETI,
&&L_OP_SETFIELD,
&&L_OP_NEWTABLE,
&&L_OP_SELF,
&&L_OP_ADDI,
&&L_OP_ADDK,
&&L_OP_SUBK,
&&L_OP_MULK,
&&L_OP_MODK,
&&L_OP_POWK,
&&L_OP_DIVK,
&&L_OP_IDIVK,
&&L_OP_BANDK,
&&L_OP_BORK,
&&L_OP_BXORK,
&&L_OP_SHRI,
&&L_OP_SHLI,
&&L_OP_ADD,
&&L_OP_SUB,
&&L_OP_MUL,
&&L_OP_MOD,
&&L_OP_POW,
&&L_OP_DIV,
&&L_OP_IDIV,
&&L_OP_BAND,
&&L_OP_BOR,
&&L_OP_BXOR,
&&L_OP_SHL,
&&L_OP_SHR,
&&L_OP_MMBIN,
&&L_OP_MMBINI,
&&L_OP_MMBINK,
&&L_OP_UNM,
&&L_OP_BNOT,
&&L_OP_NOT,
&&L_OP_LEN,
&&L_OP_CONCAT,
&&L_OP_CLOSE,
&&L_OP_TBC,
&&L_OP_JMP,
&&L_OP_EQ,
&&L_OP_LT,
&&L_OP_LE,
&&L_OP_EQK,
&&L_OP_EQI,
&&L_OP_LTI,
&&L_OP_LEI,
&&L_OP_GTI,
&&L_OP_GEI,
&&L_OP_TEST,
&&L_OP_TESTSET,
&&L_OP_CALL,
&&L_OP_TAILCALL,
&&L_OP_RETURN,
&&L_OP_RETURN0,
&&L_OP_RETURN1,
&&L_OP_FORLOOP,
&&L_OP_FORPREP,
&&L_OP_TFORPREP,
&&L_OP_TFORCALL,
&&L_OP_TFORLOOP,
&&L_OP_SETLIST,
&&L_OP_CLOSURE,
&&L_OP_VARARG,
&&L_OP_VARARGPREP,
&&L_OP_EXTRAARG
};

View file

@ -51,14 +51,14 @@ static const char *const luaX_tokens [] = {
#define save_and_next(ls) (save(ls, ls->current), next(ls))
static l_noret lexerror (LexState *ls, const char *msg, int token);
static l_noret lexerror(LexState *ls, const char *msg, int token);
static void save (LexState *ls, int c) {
static void save(LexState *ls, int c) {
Mbuffer *b = ls->buff;
if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
size_t newsize;
if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
if (luaZ_sizebuffer(b) >= MAX_SIZE / 2)
lexerror(ls, "lexical element too long", 0);
newsize = luaZ_sizebuffer(b) * 2;
luaZ_resizebuffer(ls->L, b, newsize);
@ -67,26 +67,25 @@ static void save (LexState *ls, int c) {
}
void luaX_init (lua_State *L) {
void luaX_init(lua_State *L) {
int i;
TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */
luaC_fix(L, obj2gco(e)); /* never collect this name */
for (i=0; i<NUM_RESERVED; i++) {
for (i = 0; i < NUM_RESERVED; i++) {
TString *ts = luaS_new(L, luaX_tokens[i]);
luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */
ts->extra = cast_byte(i+1); /* reserved word */
ts->extra = cast_byte(i + 1); /* reserved word */
}
}
const char *luaX_token2str (LexState *ls, int token) {
const char *luaX_token2str(LexState *ls, int token) {
if (token < FIRST_RESERVED) { /* single-byte symbols? */
if (lisprint(token))
return luaO_pushfstring(ls->L, "'%c'", token);
else /* control character */
return luaO_pushfstring(ls->L, "'<\\%d>'", token);
}
else {
} else {
const char *s = luaX_tokens[token - FIRST_RESERVED];
if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
return luaO_pushfstring(ls->L, "'%s'", s);
@ -96,10 +95,12 @@ const char *luaX_token2str (LexState *ls, int token) {
}
static const char *txtToken (LexState *ls, int token) {
static const char *txtToken(LexState *ls, int token) {
switch (token) {
case TK_NAME: case TK_STRING:
case TK_FLT: case TK_INT:
case TK_NAME:
case TK_STRING:
case TK_FLT:
case TK_INT:
save(ls, '\0');
return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff));
default:
@ -108,7 +109,7 @@ static const char *txtToken (LexState *ls, int token) {
}
static l_noret lexerror (LexState *ls, const char *msg, int token) {
static l_noret lexerror(LexState *ls, const char *msg, int token) {
msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
if (token)
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
@ -116,7 +117,7 @@ static l_noret lexerror (LexState *ls, const char *msg, int token) {
}
l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
l_noret luaX_syntaxerror(LexState *ls, const char *msg) {
lexerror(ls, msg, ls->t.token);
}
@ -131,7 +132,7 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
** is a TValue readily available. Later, the code generation can change
** this value.
*/
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
TString *luaX_newstring(LexState *ls, const char *str, size_t l) {
lua_State *L = ls->L;
TString *ts = luaS_newlstr(L, str, l); /* create new string */
const TValue *o = luaH_getstr(ls->h, ts);
@ -153,7 +154,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
** increment line number and skips newline sequence (any of
** \n, \r, \n\r, or \r\n)
*/
static void inclinenumber (LexState *ls) {
static void inclinenumber(LexState *ls) {
int old = ls->current;
lua_assert(currIsNewline(ls));
next(ls); /* skip '\n' or '\r' */
@ -164,7 +165,7 @@ static void inclinenumber (LexState *ls) {
}
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, TString *source,
int firstchar) {
ls->t.token = 0;
ls->L = L;
@ -188,12 +189,11 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
*/
static int check_next1 (LexState *ls, int c) {
static int check_next1(LexState *ls, int c) {
if (ls->current == c) {
next(ls);
return 1;
}
else return 0;
} else return 0;
}
@ -201,13 +201,12 @@ static int check_next1 (LexState *ls, int c) {
** Check whether current char is in set 'set' (with two chars) and
** saves it
*/
static int check_next2 (LexState *ls, const char *set) {
static int check_next2(LexState *ls, const char *set) {
lua_assert(set[2] == '\0');
if (ls->current == set[0] || ls->current == set[1]) {
save_and_next(ls);
return 1;
}
else return 0;
} else return 0;
}
@ -224,7 +223,7 @@ static int check_next2 (LexState *ls, const char *set) {
**
** The caller might have already read an initial dot.
*/
static int read_numeral (LexState *ls, SemInfo *seminfo) {
static int read_numeral(LexState *ls, SemInfo *seminfo) {
TValue obj;
const char *expo = "Ee";
int first = ls->current;
@ -247,8 +246,7 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
if (ttisinteger(&obj)) {
seminfo->i = ivalue(&obj);
return TK_INT;
}
else {
} else {
lua_assert(ttisfloat(&obj));
seminfo->r = fltvalue(&obj);
return TK_FLT;
@ -262,7 +260,7 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
** return 1 if it is a single bracket (no '='s and no 2nd bracket);
** otherwise (an unfinished '[==...') return 0.
*/
static size_t skip_sep (LexState *ls) {
static size_t skip_sep(LexState *ls) {
size_t count = 0;
int s = ls->current;
lua_assert(s == '[' || s == ']');
@ -277,7 +275,7 @@ static size_t skip_sep (LexState *ls) {
}
static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
static void read_long_string(LexState *ls, SemInfo *seminfo, size_t sep) {
int line = ls->linenumber; /* initial line (for error message) */
save_and_next(ls); /* skip 2nd '[' */
if (currIsNewline(ls)) /* string starts with a newline? */
@ -298,7 +296,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
}
break;
}
case '\n': case '\r': {
case '\n':
case '\r': {
save(ls, '\n');
inclinenumber(ls);
if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
@ -309,14 +308,15 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
else next(ls);
}
}
} endloop:
}
endloop:
if (seminfo)
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
luaZ_bufflen(ls->buff) - 2 * sep);
}
static void esccheck (LexState *ls, int c, const char *msg) {
static void esccheck(LexState *ls, int c, const char *msg) {
if (!c) {
if (ls->current != EOZ)
save_and_next(ls); /* add current to buffer for error message */
@ -325,14 +325,14 @@ static void esccheck (LexState *ls, int c, const char *msg) {
}
static int gethexa (LexState *ls) {
static int gethexa(LexState *ls) {
save_and_next(ls);
esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected");
esccheck(ls, lisxdigit(ls->current), "hexadecimal digit expected");
return luaO_hexavalue(ls->current);
}
static int readhexaesc (LexState *ls) {
static int readhexaesc(LexState *ls) {
int r = gethexa(ls);
r = (r << 4) + gethexa(ls);
luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */
@ -340,7 +340,7 @@ static int readhexaesc (LexState *ls) {
}
static unsigned long readutf8esc (LexState *ls) {
static unsigned long readutf8esc(LexState *ls) {
unsigned long r;
int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */
save_and_next(ls); /* skip 'u' */
@ -358,7 +358,7 @@ static unsigned long readutf8esc (LexState *ls) {
}
static void utf8esc (LexState *ls) {
static void utf8esc(LexState *ls) {
char buff[UTF8BUFFSZ];
int n = luaO_utf8esc(buff, readutf8esc(ls));
for (; n > 0; n--) /* add 'buff' to string */
@ -366,11 +366,11 @@ static void utf8esc (LexState *ls) {
}
static int readdecesc (LexState *ls) {
static int readdecesc(LexState *ls) {
int i;
int r = 0; /* result accumulator */
for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
r = 10*r + ls->current - '0';
r = 10 * r + ls->current - '0';
save_and_next(ls);
}
esccheck(ls, r <= UCHAR_MAX, "decimal escape too large");
@ -379,7 +379,7 @@ static int readdecesc (LexState *ls) {
}
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
static void read_string(LexState *ls, int del, SemInfo *seminfo) {
save_and_next(ls); /* keep delimiter (for error messages) */
while (ls->current != del) {
switch (ls->current) {
@ -394,20 +394,45 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
int c; /* final character to be saved */
save_and_next(ls); /* keep '\\' for error messages */
switch (ls->current) {
case 'a': c = '\a'; goto read_save;
case 'b': c = '\b'; goto read_save;
case 'f': c = '\f'; goto read_save;
case 'n': c = '\n'; goto read_save;
case 'r': c = '\r'; goto read_save;
case 't': c = '\t'; goto read_save;
case 'v': c = '\v'; goto read_save;
case 'x': c = readhexaesc(ls); goto read_save;
case 'u': utf8esc(ls); goto no_save;
case '\n': case '\r':
inclinenumber(ls); c = '\n'; goto only_save;
case '\\': case '\"': case '\'':
c = ls->current; goto read_save;
case EOZ: goto no_save; /* will raise an error next loop */
case 'a':
c = '\a';
goto read_save;
case 'b':
c = '\b';
goto read_save;
case 'f':
c = '\f';
goto read_save;
case 'n':
c = '\n';
goto read_save;
case 'r':
c = '\r';
goto read_save;
case 't':
c = '\t';
goto read_save;
case 'v':
c = '\v';
goto read_save;
case 'x':
c = readhexaesc(ls);
goto read_save;
case 'u':
utf8esc(ls);
goto no_save;
case '\n':
case '\r':
inclinenumber(ls);
c = '\n';
goto only_save;
case '\\':
case '\"':
case '\'':
c = ls->current;
goto read_save;
case EOZ:
goto no_save; /* will raise an error next loop */
case 'z': { /* zap following span of spaces */
luaZ_buffremove(ls->buff, 1); /* remove '\\' */
next(ls); /* skip the 'z' */
@ -423,14 +448,15 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
goto only_save;
}
}
read_save:
read_save:
next(ls);
/* go through */
only_save:
only_save:
luaZ_buffremove(ls->buff, 1); /* remove '\\' */
save(ls, c);
/* go through */
no_save: break;
no_save:
break;
}
default:
save_and_next(ls);
@ -442,15 +468,19 @@ static void read_string (LexState *ls, int del, SemInfo *seminfo) {
}
static int llex (LexState *ls, SemInfo *seminfo) {
static int llex(LexState *ls, SemInfo *seminfo) {
luaZ_resetbuffer(ls->buff);
for (;;) {
switch (ls->current) {
case '\n': case '\r': { /* line breaks */
case '\n':
case '\r': { /* line breaks */
inclinenumber(ls);
break;
}
case ' ': case '\f': case '\t': case '\v': { /* spaces */
case ' ':
case '\f':
case '\t':
case '\v': { /* spaces */
next(ls);
break;
}
@ -478,8 +508,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
if (sep >= 2) {
read_long_string(ls, seminfo, sep);
return TK_STRING;
}
else if (sep == 0) /* '[=...' missing second bracket? */
} else if (sep == 0) /* '[=...' missing second bracket? */
lexerror(ls, "invalid long string delimiter", TK_STRING);
return '[';
}
@ -515,7 +544,8 @@ static int llex (LexState *ls, SemInfo *seminfo) {
if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */
else return ':';
}
case '"': case '\'': { /* short literal strings */
case '"':
case '\'': { /* short literal strings */
read_string(ls, ls->current, seminfo);
return TK_STRING;
}
@ -525,12 +555,19 @@ static int llex (LexState *ls, SemInfo *seminfo) {
if (check_next1(ls, '.'))
return TK_DOTS; /* '...' */
else return TK_CONCAT; /* '..' */
}
else if (!lisdigit(ls->current)) return '.';
} else if (!lisdigit(ls->current)) return '.';
else return read_numeral(ls, seminfo);
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
return read_numeral(ls, seminfo);
}
case EOZ: {
@ -550,8 +587,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
else {
return TK_NAME;
}
}
else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */
} else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */
int c = ls->current;
next(ls);
return c;
@ -562,18 +598,17 @@ static int llex (LexState *ls, SemInfo *seminfo) {
}
void luaX_next (LexState *ls) {
void luaX_next(LexState *ls) {
ls->lastline = ls->linenumber;
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
ls->t = ls->lookahead; /* use this one */
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else
} else
ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
}
int luaX_lookahead (LexState *ls) {
int luaX_lookahead(LexState *ls) {
lua_assert(ls->lookahead.token == TK_EOS);
ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
return ls->lookahead.token;

View file

@ -78,14 +78,14 @@ typedef struct LexState {
} LexState;
LUAI_FUNC void luaX_init (lua_State *L);
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
LUAI_FUNC void luaX_init(lua_State *L);
LUAI_FUNC void luaX_setinput(lua_State *L, LexState *ls, ZIO *z,
TString *source, int firstchar);
LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
LUAI_FUNC void luaX_next (LexState *ls);
LUAI_FUNC int luaX_lookahead (LexState *ls);
LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
LUAI_FUNC TString *luaX_newstring(LexState *ls, const char *str, size_t l);
LUAI_FUNC void luaX_next(LexState *ls);
LUAI_FUNC int luaX_lookahead(LexState *ls);
LUAI_FUNC l_noret luaX_syntaxerror(LexState *ls, const char *s);
LUAI_FUNC const char *luaX_token2str(LexState *ls, int token);
#endif

View file

@ -26,43 +26,42 @@
#define PI (l_mathop(3.141592653589793238462643383279502884))
static int math_abs (lua_State *L) {
static int math_abs(lua_State *L) {
if (lua_isinteger(L, 1)) {
lua_Integer n = lua_tointeger(L, 1);
if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
lua_pushinteger(L, n);
}
else
} else
lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
return 1;
}
static int math_sin (lua_State *L) {
static int math_sin(lua_State *L) {
lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
return 1;
}
static int math_cos (lua_State *L) {
static int math_cos(lua_State *L) {
lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
return 1;
}
static int math_tan (lua_State *L) {
static int math_tan(lua_State *L) {
lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
return 1;
}
static int math_asin (lua_State *L) {
static int math_asin(lua_State *L) {
lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
return 1;
}
static int math_acos (lua_State *L) {
static int math_acos(lua_State *L) {
lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
return 1;
}
static int math_atan (lua_State *L) {
static int math_atan(lua_State *L) {
lua_Number y = luaL_checknumber(L, 1);
lua_Number x = luaL_optnumber(L, 2, 1);
lua_pushnumber(L, l_mathop(atan2)(y, x));
@ -70,7 +69,7 @@ static int math_atan (lua_State *L) {
}
static int math_toint (lua_State *L) {
static int math_toint(lua_State *L) {
int valid;
lua_Integer n = lua_tointegerx(L, 1, &valid);
if (l_likely(valid))
@ -83,7 +82,7 @@ static int math_toint (lua_State *L) {
}
static void pushnumint (lua_State *L, lua_Number d) {
static void pushnumint(lua_State *L, lua_Number d) {
lua_Integer n;
if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
lua_pushinteger(L, n); /* result is integer */
@ -92,7 +91,7 @@ static void pushnumint (lua_State *L, lua_Number d) {
}
static int math_floor (lua_State *L) {
static int math_floor(lua_State *L) {
if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own floor */
else {
@ -103,7 +102,7 @@ static int math_floor (lua_State *L) {
}
static int math_ceil (lua_State *L) {
static int math_ceil(lua_State *L) {
if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own ceil */
else {
@ -114,17 +113,15 @@ static int math_ceil (lua_State *L) {
}
static int math_fmod (lua_State *L) {
static int math_fmod(lua_State *L) {
if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
lua_Integer d = lua_tointeger(L, 2);
if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
luaL_argcheck(L, d != 0, 2, "zero");
lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
}
else
} else
lua_pushinteger(L, lua_tointeger(L, 1) % d);
}
else
} else
lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
luaL_checknumber(L, 2)));
return 1;
@ -136,12 +133,11 @@ static int math_fmod (lua_State *L) {
** (which is not compatible with 'float*') when lua_Number is not
** 'double'.
*/
static int math_modf (lua_State *L) {
if (lua_isinteger(L ,1)) {
static int math_modf(lua_State *L) {
if (lua_isinteger(L, 1)) {
lua_settop(L, 1); /* number is its own integer part */
lua_pushnumber(L, 0); /* no fractional part */
}
else {
} else {
lua_Number n = luaL_checknumber(L, 1);
/* integer part (rounds toward zero) */
lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
@ -153,20 +149,20 @@ static int math_modf (lua_State *L) {
}
static int math_sqrt (lua_State *L) {
static int math_sqrt(lua_State *L) {
lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
return 1;
}
static int math_ult (lua_State *L) {
static int math_ult(lua_State *L) {
lua_Integer a = luaL_checkinteger(L, 1);
lua_Integer b = luaL_checkinteger(L, 2);
lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
return 1;
}
static int math_log (lua_State *L) {
static int math_log(lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number res;
if (lua_isnoneornil(L, 2))
@ -181,29 +177,29 @@ static int math_log (lua_State *L) {
if (base == l_mathop(10.0))
res = l_mathop(log10)(x);
else
res = l_mathop(log)(x)/l_mathop(log)(base);
res = l_mathop(log)(x) / l_mathop(log)(base);
}
lua_pushnumber(L, res);
return 1;
}
static int math_exp (lua_State *L) {
static int math_exp(lua_State *L) {
lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
return 1;
}
static int math_deg (lua_State *L) {
static int math_deg(lua_State *L) {
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
return 1;
}
static int math_rad (lua_State *L) {
static int math_rad(lua_State *L) {
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
return 1;
}
static int math_min (lua_State *L) {
static int math_min(lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int imin = 1; /* index of current minimum value */
int i;
@ -217,7 +213,7 @@ static int math_min (lua_State *L) {
}
static int math_max (lua_State *L) {
static int math_max(lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int imax = 1; /* index of current maximum value */
int i;
@ -231,7 +227,7 @@ static int math_max (lua_State *L) {
}
static int math_type (lua_State *L) {
static int math_type(lua_State *L) {
if (lua_type(L, 1) == LUA_TNUMBER)
lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
else {
@ -313,11 +309,11 @@ static int math_type (lua_State *L) {
/* rotate left 'x' by 'n' bits */
static Rand64 rotl (Rand64 x, int n) {
static Rand64 rotl(Rand64 x, int n) {
return (x << n) | (trim64(x) >> (64 - n));
}
static Rand64 nextrand (Rand64 *state) {
static Rand64 nextrand(Rand64 *state) {
Rand64 state0 = state[0];
Rand64 state1 = state[1];
Rand64 state2 = state[2] ^ state0;
@ -348,7 +344,7 @@ static Rand64 nextrand (Rand64 *state) {
/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */
#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
static lua_Number I2d (Rand64 x) {
static lua_Number I2d(Rand64 x) {
SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG);
lua_Number res = (lua_Number)(sx) * scaleFIG;
if (sx < 0)
@ -398,7 +394,7 @@ typedef struct Rand64 {
*/
/* build a new Rand64 value */
static Rand64 packI (lu_int32 h, lu_int32 l) {
static Rand64 packI(lu_int32 h, lu_int32 l) {
Rand64 result;
result.h = h;
result.l = l;
@ -406,19 +402,19 @@ static Rand64 packI (lu_int32 h, lu_int32 l) {
}
/* return i << n */
static Rand64 Ishl (Rand64 i, int n) {
static Rand64 Ishl(Rand64 i, int n) {
lua_assert(n > 0 && n < 32);
return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
}
/* i1 ^= i2 */
static void Ixor (Rand64 *i1, Rand64 i2) {
static void Ixor(Rand64 *i1, Rand64 i2) {
i1->h ^= i2.h;
i1->l ^= i2.l;
}
/* return i1 + i2 */
static Rand64 Iadd (Rand64 i1, Rand64 i2) {
static Rand64 Iadd(Rand64 i1, Rand64 i2) {
Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
if (trim32(result.l) < trim32(i1.l)) /* carry? */
result.h++;
@ -426,24 +422,24 @@ static Rand64 Iadd (Rand64 i1, Rand64 i2) {
}
/* return i * 5 */
static Rand64 times5 (Rand64 i) {
static Rand64 times5(Rand64 i) {
return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
}
/* return i * 9 */
static Rand64 times9 (Rand64 i) {
static Rand64 times9(Rand64 i) {
return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
}
/* return 'i' rotated left 'n' bits */
static Rand64 rotl (Rand64 i, int n) {
static Rand64 rotl(Rand64 i, int n) {
lua_assert(n > 0 && n < 32);
return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
(trim32(i.h) >> (32 - n)) | (i.l << n));
}
/* for offsets larger than 32, rotate right by 64 - offset */
static Rand64 rotl1 (Rand64 i, int n) {
static Rand64 rotl1(Rand64 i, int n) {
lua_assert(n > 32 && n < 64);
n = 64 - n;
return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
@ -453,7 +449,7 @@ static Rand64 rotl1 (Rand64 i, int n) {
/*
** implementation of 'xoshiro256**' algorithm on 'Rand64' values
*/
static Rand64 nextrand (Rand64 *state) {
static Rand64 nextrand(Rand64 *state) {
Rand64 res = times9(rotl(times5(state[1]), 7));
Rand64 t = Ishl(state[1], 17);
Ixor(&state[2], state[0]);
@ -483,7 +479,7 @@ static Rand64 nextrand (Rand64 *state) {
** get up to 32 bits from higher half, shifting right to
** throw out the extra bits.
*/
static lua_Number I2d (Rand64 x) {
static lua_Number I2d(Rand64 x) {
lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS));
return h * scaleFIG;
}
@ -506,7 +502,7 @@ static lua_Number I2d (Rand64 x) {
#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0))
static lua_Number I2d (Rand64 x) {
static lua_Number I2d(Rand64 x) {
lua_Number h = (lua_Number)trim32(x.h) * shiftHI;
lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW);
return (h + l) * scaleFIG;
@ -516,12 +512,12 @@ static lua_Number I2d (Rand64 x) {
/* convert a 'Rand64' to a 'lua_Unsigned' */
static lua_Unsigned I2UInt (Rand64 x) {
static lua_Unsigned I2UInt(Rand64 x) {
return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l);
}
/* convert a 'lua_Unsigned' to a 'Rand64' */
static Rand64 Int2I (lua_Unsigned n) {
static Rand64 Int2I(lua_Unsigned n) {
return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n);
}
@ -546,7 +542,7 @@ typedef struct {
** 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,
static lua_Unsigned project(lua_Unsigned ran, lua_Unsigned n,
RanState *state) {
if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */
return ran & n; /* no bias */
@ -571,7 +567,7 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
}
static int math_random (lua_State *L) {
static int math_random(lua_State *L) {
lua_Integer low, up;
lua_Unsigned p;
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
@ -595,7 +591,8 @@ static int math_random (lua_State *L) {
up = luaL_checkinteger(L, 2);
break;
}
default: return luaL_error(L, "wrong number of arguments");
default:
return luaL_error(L, "wrong number of arguments");
}
/* random integer in the interval [low, up] */
luaL_argcheck(L, low <= up, 1, "interval is empty");
@ -606,7 +603,7 @@ static int math_random (lua_State *L) {
}
static void setseed (lua_State *L, Rand64 *state,
static void setseed(lua_State *L, Rand64 *state,
lua_Unsigned n1, lua_Unsigned n2) {
int i;
state[0] = Int2I(n1);
@ -625,19 +622,18 @@ static void setseed (lua_State *L, Rand64 *state,
** and the address of 'L' (in case the machine does address space layout
** randomization).
*/
static void randseed (lua_State *L, RanState *state) {
static void randseed(lua_State *L, RanState *state) {
lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
setseed(L, state->s, seed1, seed2);
}
static int math_randomseed (lua_State *L) {
static int math_randomseed(lua_State *L) {
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
if (lua_isnone(L, 1)) {
randseed(L, state);
}
else {
} else {
lua_Integer n1 = luaL_checkinteger(L, 1);
lua_Integer n2 = luaL_optinteger(L, 2, 0);
setseed(L, state->s, n1, n2);
@ -656,7 +652,7 @@ static const luaL_Reg randfuncs[] = {
/*
** Register the random functions and initialize their state.
*/
static void setrandfunc (lua_State *L) {
static void setrandfunc(lua_State *L) {
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
randseed(L, state); /* initialize with a "random" seed */
lua_pop(L, 2); /* remove pushed seeds */
@ -673,43 +669,43 @@ static void setrandfunc (lua_State *L) {
*/
#if defined(LUA_COMPAT_MATHLIB)
static int math_cosh (lua_State *L) {
static int math_cosh(lua_State *L) {
lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_sinh (lua_State *L) {
static int math_sinh(lua_State *L) {
lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_tanh (lua_State *L) {
static int math_tanh(lua_State *L) {
lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
return 1;
}
static int math_pow (lua_State *L) {
static int math_pow(lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2);
lua_pushnumber(L, l_mathop(pow)(x, y));
return 1;
}
static int math_frexp (lua_State *L) {
static int math_frexp(lua_State *L) {
int e;
lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
lua_pushinteger(L, e);
return 2;
}
static int math_ldexp (lua_State *L) {
static int math_ldexp(lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
int ep = (int)luaL_checkinteger(L, 2);
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
return 1;
}
static int math_log10 (lua_State *L) {
static int math_log10(lua_State *L) {
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
return 1;
}
@ -765,7 +761,7 @@ static const luaL_Reg mathlib[] = {
/*
** Open math library
*/
LUAMOD_API int luaopen_math (lua_State *L) {
LUAMOD_API int luaopen_math(lua_State *L) {
luaL_newlib(L, mathlib);
lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi");

View file

@ -66,7 +66,7 @@
** fail) and when it cannot try again; this fail will trigger 'tryagain'
** and a full GC cycle at every allocation.
*/
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
static void *firsttry(global_State *g, void *block, size_t os, size_t ns) {
if (ns > 0 && cantryagain(g))
return NULL; /* fail */
else /* normal allocation */
@ -94,7 +94,7 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
#define MINSIZEARRAY 4
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
void *luaM_growaux_(lua_State *L, void *block, int nelems, int *psize,
int size_elems, int limit, const char *what) {
void *newblock;
int size = *psize;
@ -104,8 +104,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
if (l_unlikely(size >= limit)) /* cannot grow even a little? */
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
size = limit; /* still have at least one free place */
}
else {
} else {
size *= 2;
if (size < MINSIZEARRAY)
size = MINSIZEARRAY; /* minimum size */
@ -125,7 +124,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
** to its number of elements, the only option is to raise an
** error.
*/
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
void *luaM_shrinkvector_(lua_State *L, void *block, int *size,
int final_n, int size_elem) {
void *newblock;
size_t oldsize = cast_sizet((*size) * size_elem);
@ -139,7 +138,7 @@ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
/* }================================================================== */
l_noret luaM_toobig (lua_State *L) {
l_noret luaM_toobig(lua_State *L) {
luaG_runerror(L, "memory allocation error: block too big");
}
@ -147,7 +146,7 @@ l_noret luaM_toobig (lua_State *L) {
/*
** Free memory
*/
void luaM_free_ (lua_State *L, void *block, size_t osize) {
void luaM_free_(lua_State *L, void *block, size_t osize) {
global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL));
callfrealloc(g, block, osize, 0);
@ -159,21 +158,20 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
** In case of allocation fail, this function will do an emergency
** collection to free some memory and then try the allocation again.
*/
static void *tryagain (lua_State *L, void *block,
static void *tryagain(lua_State *L, void *block,
size_t osize, size_t nsize) {
global_State *g = G(L);
if (cantryagain(g)) {
luaC_fullgc(L, 1); /* try to free some memory... */
return callfrealloc(g, block, osize, nsize); /* try again */
}
else return NULL; /* cannot run an emergency collection */
} else return NULL; /* cannot run an emergency collection */
}
/*
** Generic allocation routine.
*/
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
void *luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize) {
void *newblock;
global_State *g = G(L);
lua_assert((osize == 0) == (block == NULL));
@ -189,7 +187,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
}
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
void *luaM_saferealloc_(lua_State *L, void *block, size_t osize,
size_t nsize) {
void *newblock = luaM_realloc_(L, block, osize, nsize);
if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
@ -198,7 +196,7 @@ void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
}
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
void *luaM_malloc_(lua_State *L, size_t size, int tag) {
if (size == 0)
return NULL; /* that's all */
else {

View file

@ -74,20 +74,20 @@
#define luaM_shrinkvector(L,v,size,fs,t) \
((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
LUAI_FUNC l_noret luaM_toobig (lua_State *L);
LUAI_FUNC l_noret luaM_toobig(lua_State *L);
/* not to be called directly */
LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
LUAI_FUNC void *luaM_realloc_(lua_State *L, void *block, size_t oldsize,
size_t size);
LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
LUAI_FUNC void *luaM_saferealloc_(lua_State *L, void *block, size_t oldsize,
size_t size);
LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
LUAI_FUNC void luaM_free_(lua_State *L, void *block, size_t osize);
LUAI_FUNC void *luaM_growaux_(lua_State *L, void *block, int nelems,
int *size, int size_elem, int limit,
const char *what);
LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
LUAI_FUNC void *luaM_shrinkvector_(lua_State *L, void *block, int *nelem,
int final_n, int size_elem);
LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
LUAI_FUNC void *luaM_malloc_(lua_State *L, size_t size, int tag);
#endif

View file

@ -72,7 +72,7 @@ typedef void (*voidf)(void);
/*
** unload library 'lib'
*/
static void lsys_unloadlib (void *lib);
static void lsys_unloadlib(void *lib);
/*
** load C library in file 'path'. If 'seeglb', load with all names in
@ -80,14 +80,14 @@ static void lsys_unloadlib (void *lib);
** Returns the library; in case of error, returns NULL plus an
** error string in the stack.
*/
static void *lsys_load (lua_State *L, const char *path, int seeglb);
static void *lsys_load(lua_State *L, const char *path, int seeglb);
/*
** Try to find a function named 'sym' in library 'lib'.
** Returns the function; in case of error, returns NULL plus an
** error string in the stack.
*/
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym);
@ -116,12 +116,12 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
#endif
static void lsys_unloadlib (void *lib) {
static void lsys_unloadlib(void *lib) {
dlclose(lib);
}
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
static void *lsys_load(lua_State *L, const char *path, int seeglb) {
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
if (l_unlikely(lib == NULL))
lua_pushstring(L, dlerror());
@ -129,7 +129,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
}
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym) {
lua_CFunction f = cast_func(dlsym(lib, sym));
if (l_unlikely(f == NULL))
lua_pushstring(L, dlerror());
@ -165,10 +165,10 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
** Replace in the path (on the top of the stack) any occurrence
** of LUA_EXEC_DIR with the executable's path.
*/
static void setprogdir (lua_State *L) {
static void setprogdir(lua_State *L) {
char buff[MAX_PATH + 1];
char *lb;
DWORD nsize = sizeof(buff)/sizeof(char);
DWORD nsize = sizeof(buff) / sizeof(char);
DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
luaL_error(L, "unable to get ModuleFileName");
@ -182,22 +182,22 @@ static void setprogdir (lua_State *L) {
static void pusherror (lua_State *L) {
static void pusherror(lua_State *L) {
int error = GetLastError();
char buffer[128];
if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
NULL, error, 0, buffer, sizeof(buffer) / sizeof(char), NULL))
lua_pushstring(L, buffer);
else
lua_pushfstring(L, "system error %d\n", error);
}
static void lsys_unloadlib (void *lib) {
static void lsys_unloadlib(void *lib) {
FreeLibrary((HMODULE)lib);
}
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
static void *lsys_load(lua_State *L, const char *path, int seeglb) {
HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
(void)(seeglb); /* not used: symbols are 'global' by default */
if (lib == NULL) pusherror(L);
@ -205,7 +205,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
}
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym) {
lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
if (f == NULL) pusherror(L);
return f;
@ -228,20 +228,22 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
#define DLMSG "dynamic libraries not enabled; check your Lua installation"
static void lsys_unloadlib (void *lib) {
static void lsys_unloadlib(void *lib) {
(void)(lib); /* not used */
}
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
(void)(path); (void)(seeglb); /* not used */
static void *lsys_load(lua_State *L, const char *path, int seeglb) {
(void)(path);
(void)(seeglb); /* not used */
lua_pushliteral(L, DLMSG);
return NULL;
}
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
(void)(lib); (void)(sym); /* not used */
static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym) {
(void)(lib);
(void)(sym); /* not used */
lua_pushliteral(L, DLMSG);
return NULL;
}
@ -273,7 +275,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
/*
** return registry.LUA_NOENV as a boolean
*/
static int noenv (lua_State *L) {
static int noenv(lua_State *L) {
int b;
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
b = lua_toboolean(L, -1);
@ -285,7 +287,7 @@ static int noenv (lua_State *L) {
/*
** Set a path
*/
static void setpath (lua_State *L, const char *fieldname,
static void setpath(lua_State *L, const char *fieldname,
const char *envname,
const char *dft) {
const char *dftmark;
@ -323,7 +325,7 @@ static void setpath (lua_State *L, const char *fieldname,
/*
** return registry.CLIBS[path]
*/
static void *checkclib (lua_State *L, const char *path) {
static void *checkclib(lua_State *L, const char *path) {
void *plib;
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
lua_getfield(L, -1, path);
@ -337,7 +339,7 @@ static void *checkclib (lua_State *L, const char *path) {
** registry.CLIBS[path] = plib -- for queries
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
*/
static void addtoclib (lua_State *L, const char *path, void *plib) {
static void addtoclib(lua_State *L, const char *path, void *plib) {
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
lua_pushlightuserdata(L, plib);
lua_pushvalue(L, -1);
@ -351,7 +353,7 @@ static void addtoclib (lua_State *L, const char *path, void *plib) {
** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
** handles in list CLIBS
*/
static int gctm (lua_State *L) {
static int gctm(lua_State *L) {
lua_Integer n = luaL_len(L, 1);
for (; n >= 1; n--) { /* for each handle, in reverse order */
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
@ -378,7 +380,7 @@ static int gctm (lua_State *L) {
** Return 0 and 'true' or a function in the stack; in case of
** errors, return an error code and an error message in the stack.
*/
static int lookforfunc (lua_State *L, const char *path, const char *sym) {
static int lookforfunc(lua_State *L, const char *path, const char *sym) {
void *reg = checkclib(L, path); /* check loaded C libraries */
if (reg == NULL) { /* must load library? */
reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
@ -388,8 +390,7 @@ static int lookforfunc (lua_State *L, const char *path, const char *sym) {
if (*sym == '*') { /* loading only library (no function)? */
lua_pushboolean(L, 1); /* return 'true' */
return 0; /* no errors */
}
else {
} else {
lua_CFunction f = lsys_sym(L, reg, sym);
if (f == NULL)
return ERRFUNC; /* unable to find function */
@ -399,7 +400,7 @@ static int lookforfunc (lua_State *L, const char *path, const char *sym) {
}
static int ll_loadlib (lua_State *L) {
static int ll_loadlib(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
const char *init = luaL_checkstring(L, 2);
int stat = lookforfunc(L, path, init);
@ -422,7 +423,7 @@ static int ll_loadlib (lua_State *L) {
*/
static int readable (const char *filename) {
static int readable(const char *filename) {
FILE *f = fopen(filename, "r"); /* try to open file */
if (f == NULL) return 0; /* open failed */
fclose(f);
@ -435,7 +436,7 @@ static int readable (const char *filename) {
** the ending ';' to '\0' to create a zero-terminated string. Return
** NULL when list ends.
*/
static const char *getnextfilename (char **path, char *end) {
static const char *getnextfilename(char **path, char *end) {
char *sep;
char *name = *path;
if (name == end)
@ -459,7 +460,7 @@ static const char *getnextfilename (char **path, char *end) {
** no file 'blabla.so'
** no file 'blublu.so'
*/
static void pusherrornotfound (lua_State *L, const char *path) {
static void pusherrornotfound(lua_State *L, const char *path) {
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addstring(&b, "no file '");
@ -469,7 +470,7 @@ static void pusherrornotfound (lua_State *L, const char *path) {
}
static const char *searchpath (lua_State *L, const char *name,
static const char *searchpath(lua_State *L, const char *name,
const char *path,
const char *sep,
const char *dirsep) {
@ -496,7 +497,7 @@ static const char *searchpath (lua_State *L, const char *name,
}
static int ll_searchpath (lua_State *L) {
static int ll_searchpath(lua_State *L) {
const char *f = searchpath(L, luaL_checkstring(L, 1),
luaL_checkstring(L, 2),
luaL_optstring(L, 3, "."),
@ -510,7 +511,7 @@ static int ll_searchpath (lua_State *L) {
}
static const char *findfile (lua_State *L, const char *name,
static const char *findfile(lua_State *L, const char *name,
const char *pname,
const char *dirsep) {
const char *path;
@ -522,18 +523,17 @@ static const char *findfile (lua_State *L, const char *name,
}
static int checkload (lua_State *L, int stat, const char *filename) {
static int checkload(lua_State *L, int stat, const char *filename) {
if (l_likely(stat)) { /* module loaded successfully? */
lua_pushstring(L, filename); /* will be 2nd argument to module */
return 2; /* return open function and file name */
}
else
} else
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1));
}
static int searcher_Lua (lua_State *L) {
static int searcher_Lua(lua_State *L) {
const char *filename;
const char *name = luaL_checkstring(L, 1);
filename = findfile(L, name, "path", LUA_LSUBSEP);
@ -550,7 +550,7 @@ static int searcher_Lua (lua_State *L) {
** fails, it also tries "luaopen_Y".) If there is no ignore mark,
** look for a function named "luaopen_modname".
*/
static int loadfunc (lua_State *L, const char *filename, const char *modname) {
static int loadfunc(lua_State *L, const char *filename, const char *modname) {
const char *openfunc;
const char *mark;
modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
@ -568,7 +568,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
}
static int searcher_C (lua_State *L) {
static int searcher_C(lua_State *L) {
const char *name = luaL_checkstring(L, 1);
const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
if (filename == NULL) return 1; /* module not found in this path */
@ -576,7 +576,7 @@ static int searcher_C (lua_State *L) {
}
static int searcher_Croot (lua_State *L) {
static int searcher_Croot(lua_State *L) {
const char *filename;
const char *name = luaL_checkstring(L, 1);
const char *p = strchr(name, '.');
@ -598,21 +598,20 @@ static int searcher_Croot (lua_State *L) {
}
static int searcher_preload (lua_State *L) {
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, "no field package.preload['%s']", name);
return 1;
}
else {
} else {
lua_pushliteral(L, ":preload:");
return 2;
}
}
static void findloader (lua_State *L, const char *name) {
static void findloader(lua_State *L, const char *name) {
int i;
luaL_Buffer msg; /* to build error message */
/* push 'package.searchers' to index 3 in the stack */
@ -636,8 +635,7 @@ static void findloader (lua_State *L, const char *name) {
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
lua_pop(L, 1); /* remove extra return */
luaL_addvalue(&msg); /* concatenate error message */
}
else { /* no error message */
} else { /* no error message */
lua_pop(L, 2); /* remove both returns */
luaL_buffsub(&msg, 2); /* remove prefix */
}
@ -645,7 +643,7 @@ static void findloader (lua_State *L, const char *name) {
}
static int ll_require (lua_State *L) {
static int ll_require(lua_State *L) {
const char *name = luaL_checkstring(L, 1);
lua_settop(L, 1); /* LOADED table will be at index 2 */
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
@ -698,7 +696,7 @@ static const luaL_Reg ll_funcs[] = {
};
static void createsearcherstable (lua_State *L) {
static void createsearcherstable(lua_State *L) {
static const lua_CFunction searchers[] = {
searcher_preload,
searcher_Lua,
@ -708,12 +706,12 @@ static void createsearcherstable (lua_State *L) {
};
int i;
/* create 'searchers' table */
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
lua_createtable(L, sizeof(searchers) / sizeof(searchers[0]) - 1, 0);
/* fill it with predefined searchers */
for (i=0; searchers[i] != NULL; i++) {
for (i = 0; searchers[i] != NULL; i++) {
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
lua_pushcclosure(L, searchers[i], 1);
lua_rawseti(L, -2, i+1);
lua_rawseti(L, -2, i + 1);
}
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
}
@ -723,7 +721,7 @@ static void createsearcherstable (lua_State *L) {
** create table CLIBS to keep track of loaded C libraries,
** setting a finalizer to close all libraries when closing state.
*/
static void createclibstable (lua_State *L) {
static void createclibstable(lua_State *L) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
lua_pushcfunction(L, gctm);
@ -732,7 +730,7 @@ static void createclibstable (lua_State *L) {
}
LUAMOD_API int luaopen_package (lua_State *L) {
LUAMOD_API int luaopen_package(lua_State *L) {
createclibstable(L);
luaL_newlib(L, pk_funcs); /* create 'package' table */
createsearcherstable(L);

View file

@ -32,16 +32,16 @@
/*
** Computes ceil(log2(x))
*/
int luaO_ceillog2 (unsigned int x) {
int luaO_ceillog2(unsigned int x) {
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
};
int l = 0;
x--;
@ -50,80 +50,107 @@ int luaO_ceillog2 (unsigned int x) {
}
static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
static lua_Integer intarith(lua_State *L, int op, lua_Integer v1,
lua_Integer v2) {
switch (op) {
case LUA_OPADD: return intop(+, v1, v2);
case LUA_OPSUB:return intop(-, v1, v2);
case LUA_OPMUL:return intop(*, v1, v2);
case LUA_OPMOD: return luaV_mod(L, v1, v2);
case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
case LUA_OPBAND: return intop(&, v1, v2);
case LUA_OPBOR: return intop(|, v1, v2);
case LUA_OPBXOR: return intop(^, v1, v2);
case LUA_OPSHL: return luaV_shiftl(v1, v2);
case LUA_OPSHR: return luaV_shiftr(v1, v2);
case LUA_OPUNM: return intop(-, 0, v1);
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
default: lua_assert(0); return 0;
case LUA_OPADD:
return intop(+, v1, v2);
case LUA_OPSUB:
return intop(-, v1, v2);
case LUA_OPMUL:
return intop(*, v1, v2);
case LUA_OPMOD:
return luaV_mod(L, v1, v2);
case LUA_OPIDIV:
return luaV_idiv(L, v1, v2);
case LUA_OPBAND:
return intop(&, v1, v2);
case LUA_OPBOR:
return intop( |, v1, v2);
case LUA_OPBXOR:
return intop(^, v1, v2);
case LUA_OPSHL:
return luaV_shiftl(v1, v2);
case LUA_OPSHR:
return luaV_shiftr(v1, v2);
case LUA_OPUNM:
return intop(-, 0, v1);
case LUA_OPBNOT:
return intop(^, ~l_castS2U(0), v1);
default:
lua_assert(0);
return 0;
}
}
static lua_Number numarith (lua_State *L, int op, lua_Number v1,
static lua_Number numarith(lua_State *L, int op, lua_Number v1,
lua_Number v2) {
switch (op) {
case LUA_OPADD: return luai_numadd(L, v1, v2);
case LUA_OPSUB: return luai_numsub(L, v1, v2);
case LUA_OPMUL: return luai_nummul(L, v1, v2);
case LUA_OPDIV: return luai_numdiv(L, v1, v2);
case LUA_OPPOW: return luai_numpow(L, v1, v2);
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
case LUA_OPUNM: return luai_numunm(L, v1);
case LUA_OPMOD: return luaV_modf(L, v1, v2);
default: lua_assert(0); return 0;
case LUA_OPADD:
return luai_numadd(L, v1, v2);
case LUA_OPSUB:
return luai_numsub(L, v1, v2);
case LUA_OPMUL:
return luai_nummul(L, v1, v2);
case LUA_OPDIV:
return luai_numdiv(L, v1, v2);
case LUA_OPPOW:
return luai_numpow(L, v1, v2);
case LUA_OPIDIV:
return luai_numidiv(L, v1, v2);
case LUA_OPUNM:
return luai_numunm(L, v1);
case LUA_OPMOD:
return luaV_modf(L, v1, v2);
default:
lua_assert(0);
return 0;
}
}
int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
int luaO_rawarith(lua_State *L, int op, const TValue *p1, const TValue *p2,
TValue *res) {
switch (op) {
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
case LUA_OPSHL: case LUA_OPSHR:
case LUA_OPBAND:
case LUA_OPBOR:
case LUA_OPBXOR:
case LUA_OPSHL:
case LUA_OPSHR:
case LUA_OPBNOT: { /* operate only on integers */
lua_Integer i1; lua_Integer i2;
lua_Integer i1;
lua_Integer i2;
if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
setivalue(res, intarith(L, op, i1, i2));
return 1;
} else return 0; /* fail */
}
else return 0; /* fail */
}
case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
lua_Number n1; lua_Number n2;
case LUA_OPDIV:
case LUA_OPPOW: { /* operate only on floats */
lua_Number n1;
lua_Number n2;
if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; /* fail */
} else return 0; /* fail */
}
default: { /* other operations */
lua_Number n1; lua_Number n2;
lua_Number n1;
lua_Number n2;
if (ttisinteger(p1) && ttisinteger(p2)) {
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
return 1;
}
else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
} else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
setfltvalue(res, numarith(L, op, n1, n2));
return 1;
}
else return 0; /* fail */
} else return 0; /* fail */
}
}
}
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
void luaO_arith(lua_State *L, int op, const TValue *p1, const TValue *p2,
StkId res) {
if (!luaO_rawarith(L, op, p1, p2, s2v(res))) {
/* could not perform raw operation; try metamethod */
@ -132,15 +159,15 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
}
int luaO_hexavalue (int c) {
int luaO_hexavalue(int c) {
if (lisdigit(c)) return c - '0';
else return (ltolower(c) - 'a') + 10;
}
static int isneg (const char **s) {
static int isneg(const char **s) {
if (**s == '-') { (*s)++; return 1; }
else if (**s == '+') (*s)++;
else if (**s == '+')(*s)++;
return 0;
}
@ -162,7 +189,7 @@ static int isneg (const char **s) {
** convert a hexadecimal numeric string to a number, following
** C99 specification for 'strtod'
*/
static lua_Number lua_strx2number (const char *s, char **endptr) {
static lua_Number lua_strx2number(const char *s, char **endptr) {
int dot = lua_getlocaledecpoint();
lua_Number r = l_mathop(0.0); /* result (accumulator) */
int sigdig = 0; /* number of significant digits */
@ -179,16 +206,14 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
if (*s == dot) {
if (hasdot) break; /* second dot? stop loop */
else hasdot = 1;
}
else if (lisxdigit(cast_uchar(*s))) {
} else if (lisxdigit(cast_uchar(*s))) {
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
nosigdig++;
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
r = (r * l_mathop(16.0)) + luaO_hexavalue(*s);
else e++; /* too many digits; ignore, but still count for exponent */
if (hasdot) e--; /* decimal digit? correct exponent */
}
else break; /* neither a dot nor a digit */
} else break; /* neither a dot nor a digit */
}
if (nosigdig + sigdig == 0) /* no digits? */
return l_mathop(0.0); /* invalid format */
@ -225,7 +250,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
** fail or the address of the ending '\0' on success. ('mode' == 'x')
** means a hexadecimal numeral.
*/
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
static const char *l_str2dloc(const char *s, lua_Number *result, int mode) {
char *endptr;
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
: lua_str2number(s, &endptr);
@ -248,7 +273,7 @@ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
** - 'x' means a hexadecimal numeral
** - '.' just optimizes the search for the common case (no special chars)
*/
static const char *l_str2d (const char *s, lua_Number *result) {
static const char *l_str2d(const char *s, lua_Number *result) {
const char *endptr;
const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
@ -273,7 +298,7 @@ static const char *l_str2d (const char *s, lua_Number *result) {
#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
#define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
static const char *l_str2int (const char *s, lua_Integer *result) {
static const char *l_str2int(const char *s, lua_Integer *result) {
lua_Unsigned a = 0;
int empty = 1;
int neg;
@ -286,8 +311,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
a = a * 16 + luaO_hexavalue(*s);
empty = 0;
}
}
else { /* decimal */
} else { /* decimal */
for (; lisdigit(cast_uchar(*s)); s++) {
int d = *s - '0';
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
@ -305,22 +329,21 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
}
size_t luaO_str2num (const char *s, TValue *o) {
lua_Integer i; lua_Number n;
size_t luaO_str2num(const char *s, TValue *o) {
lua_Integer i;
lua_Number n;
const char *e;
if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
setivalue(o, i);
}
else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
} else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
setfltvalue(o, n);
}
else
} else
return 0; /* conversion failed */
return (e - s) + 1; /* success; return string size */
}
int luaO_utf8esc (char *buff, unsigned long x) {
int luaO_utf8esc(char *buff, unsigned long x) {
int n = 1; /* number of bytes put in buffer (backwards) */
lua_assert(x <= 0x7FFFFFFFu);
if (x < 0x80) /* ascii? */
@ -352,7 +375,7 @@ int luaO_utf8esc (char *buff, unsigned long x) {
/*
** Convert a number object to a string, adding it to a buffer
*/
static int tostringbuff (TValue *obj, char *buff) {
static int tostringbuff(TValue *obj, char *buff) {
int len;
lua_assert(ttisnumber(obj));
if (ttisinteger(obj))
@ -371,7 +394,7 @@ static int tostringbuff (TValue *obj, char *buff) {
/*
** Convert a number object to a Lua string, replacing the value at 'obj'
*/
void luaO_tostring (lua_State *L, TValue *obj) {
void luaO_tostring(lua_State *L, TValue *obj) {
char buff[MAXNUMBER2STR];
int len = tostringbuff(obj, buff);
setsvalue(L, obj, luaS_newlstr(L, buff, len));
@ -411,7 +434,7 @@ typedef struct BuffFS {
** long. In that case, 'luaV_concat' frees the extra slot before
** raising the error.
*/
static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
static void pushstr(BuffFS *buff, const char *str, size_t lstr) {
lua_State *L = buff->L;
setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr));
L->top.p++; /* may use one slot from EXTRA_STACK */
@ -425,7 +448,7 @@ static void pushstr (BuffFS *buff, const char *str, size_t lstr) {
/*
** empty the buffer space into the stack
*/
static void clearbuff (BuffFS *buff) {
static void clearbuff(BuffFS *buff) {
pushstr(buff, buff->space, buff->blen); /* push buffer contents */
buff->blen = 0; /* space now is empty */
}
@ -435,8 +458,9 @@ static void clearbuff (BuffFS *buff) {
** Get a space of size 'sz' in the buffer. If buffer has not enough
** space, empty it. 'sz' must fit in an empty buffer.
*/
static char *getbuff (BuffFS *buff, int sz) {
lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
static char *getbuff(BuffFS *buff, int sz) {
lua_assert(buff->blen <= BUFVFS);
lua_assert(sz <= BUFVFS);
if (sz > BUFVFS - buff->blen) /* not enough space? */
clearbuff(buff);
return buff->space + buff->blen;
@ -450,13 +474,12 @@ static char *getbuff (BuffFS *buff, int sz) {
** Add 'str' to the buffer. If string is larger than the buffer space,
** push the string directly to the stack.
*/
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
static void addstr2buff(BuffFS *buff, const char *str, size_t slen) {
if (slen <= BUFVFS) { /* does string fit into buffer? */
char *bf = getbuff(buff, cast_int(slen));
memcpy(bf, str, slen); /* add string to buffer */
addsize(buff, cast_int(slen));
}
else { /* string larger than buffer */
} else { /* string larger than buffer */
clearbuff(buff); /* string comes after buffer's content */
pushstr(buff, str, slen); /* push string */
}
@ -466,7 +489,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
/*
** Add a numeral to the buffer.
*/
static void addnum2buff (BuffFS *buff, TValue *num) {
static void addnum2buff(BuffFS *buff, TValue *num) {
char *numbuff = getbuff(buff, MAXNUMBER2STR);
int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
addsize(buff, len);
@ -477,7 +500,7 @@ static void addnum2buff (BuffFS *buff, TValue *num) {
** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
conventional formats, plus Lua-specific '%I' and '%U'
*/
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
const char *luaO_pushvfstring(lua_State *L, const char *fmt, va_list argp) {
BuffFS buff; /* holds last part of the result */
const char *e; /* points to next '%' */
buff.pushed = buff.blen = 0;
@ -515,7 +538,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
break;
}
case 'p': { /* a pointer */
const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
const int sz = 3 * sizeof(void *) + 8; /* enough space for '%p' */
char *bf = getbuff(&buff, sz);
void *p = va_arg(argp, void *);
int len = lua_pointer2str(bf, sz, p);
@ -546,7 +569,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
}
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
const char *luaO_pushfstring(lua_State *L, const char *fmt, ...) {
const char *msg;
va_list argp;
va_start(argp, fmt);
@ -564,7 +587,7 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
void luaO_chunkid (char *out, const char *source, size_t srclen) {
void luaO_chunkid(char *out, const char *source, size_t srclen) {
size_t bufflen = LUA_IDSIZE; /* free space in buffer */
if (*source == '=') { /* 'literal' source */
if (srclen <= bufflen) /* small enough? */
@ -573,8 +596,7 @@ void luaO_chunkid (char *out, const char *source, size_t srclen) {
addstr(out, source + 1, bufflen - 1);
*out = '\0';
}
}
else if (*source == '@') { /* file name */
} else if (*source == '@') { /* file name */
if (srclen <= bufflen) /* small enough? */
memcpy(out, source + 1, srclen * sizeof(char));
else { /* add '...' before rest of name */
@ -582,15 +604,13 @@ void luaO_chunkid (char *out, const char *source, size_t srclen) {
bufflen -= LL(RETS);
memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
}
}
else { /* string; format as [string "source"] */
} else { /* string; format as [string "source"] */
const char *nl = strchr(source, '\n'); /* find first new line (if any) */
addstr(out, PRE, LL(PRE)); /* add prefix */
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
if (srclen < bufflen && nl == NULL) { /* small one-line source? */
addstr(out, source, srclen); /* keep it */
}
else {
} else {
if (nl != NULL) srclen = nl - source; /* stop at first newline */
if (srclen > bufflen) srclen = bufflen;
addstr(out, source, srclen);

View file

@ -794,19 +794,19 @@ typedef struct Table {
/* size of buffer for 'luaO_utf8esc' function */
#define UTF8BUFFSZ 8
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
LUAI_FUNC int luaO_utf8esc(char *buff, unsigned long x);
LUAI_FUNC int luaO_ceillog2(unsigned int x);
LUAI_FUNC int luaO_rawarith(lua_State *L, int op, const TValue *p1,
const TValue *p2, TValue *res);
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
LUAI_FUNC void luaO_arith(lua_State *L, int op, const TValue *p1,
const TValue *p2, StkId res);
LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
LUAI_FUNC int luaO_hexavalue (int c);
LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
LUAI_FUNC size_t luaO_str2num(const char *s, TValue *o);
LUAI_FUNC int luaO_hexavalue(int c);
LUAI_FUNC void luaO_tostring(lua_State *L, TValue *obj);
LUAI_FUNC const char *luaO_pushvfstring(lua_State *L, const char *fmt,
va_list argp);
LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
LUAI_FUNC const char *luaO_pushfstring(lua_State *L, const char *fmt, ...);
LUAI_FUNC void luaO_chunkid(char *out, const char *source, size_t srclen);
#endif

View file

@ -16,89 +16,89 @@
/* ORDER OP */
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
/* MM OT IT T A mode opcode */
/* MM OT IT T A mode opcode */
opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/
,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */
,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */
,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */
,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */
,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */
,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */
,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */
,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */
,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
, opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
, opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
, opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
, opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
, opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
, opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/
, opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/
, opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */
, opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */
, opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */
, opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */
, opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */
, opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */
, opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */
, opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */
, opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */
, opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */
, opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
, opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
, opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
, opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */
, opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
, opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
, opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
, opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
};

View file

@ -195,118 +195,118 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
*/
typedef enum {
/*----------------------------------------------------------------------
/*----------------------------------------------------------------------
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_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_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]:shortstring] */
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]:shortstring] */
OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */
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]:shortstring] */
OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := 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]:shortstring] := RK(C) */
OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := 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]:shortstring] := RK(C) */
OP_NEWTABLE,/* A B C k 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]:number */
OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
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 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_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] := #R[B] (length operator) */
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] := #R[B] (length operator) */
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_TBC,/* A mark variable A "to be closed" */
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_CLOSE,/* A close all upvalues >= R[A] */
OP_TBC,/* A mark variable A "to be closed" */
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 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_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 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_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 k 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 k 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>;
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 k R[A][C+i] := R[A+i], 1 <= i <= B */
OP_SETLIST,/* A B C k R[A][C+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 */
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
} OpCode;

View file

@ -139,7 +139,7 @@
#endif
static int os_execute (lua_State *L) {
static int os_execute(lua_State *L) {
const char *cmd = luaL_optstring(L, 1, NULL);
int stat;
errno = 0;
@ -153,14 +153,14 @@ static int os_execute (lua_State *L) {
}
static int os_remove (lua_State *L) {
static int os_remove(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
errno = 0;
return luaL_fileresult(L, remove(filename) == 0, filename);
}
static int os_rename (lua_State *L) {
static int os_rename(lua_State *L) {
const char *fromname = luaL_checkstring(L, 1);
const char *toname = luaL_checkstring(L, 2);
errno = 0;
@ -168,7 +168,7 @@ static int os_rename (lua_State *L) {
}
static int os_tmpname (lua_State *L) {
static int os_tmpname(lua_State *L) {
char buff[LUA_TMPNAMBUFSIZE];
int err;
lua_tmpnam(buff, err);
@ -179,14 +179,14 @@ static int os_tmpname (lua_State *L) {
}
static int os_getenv (lua_State *L) {
static int os_getenv(lua_State *L) {
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
return 1;
}
static int os_clock (lua_State *L) {
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
static int os_clock(lua_State *L) {
lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)CLOCKS_PER_SEC);
return 1;
}
@ -208,17 +208,17 @@ static int os_clock (lua_State *L) {
** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
** to compute the year.
*/
static void setfield (lua_State *L, const char *key, int value, int delta) {
#if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
static void setfield(lua_State *L, const char *key, int value, int delta) {
#if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
if (l_unlikely(value > LUA_MAXINTEGER - delta))
luaL_error(L, "field '%s' is out-of-bound", key);
#endif
#endif
lua_pushinteger(L, (lua_Integer)value + delta);
lua_setfield(L, -2, key);
}
static void setboolfield (lua_State *L, const char *key, int value) {
static void setboolfield(lua_State *L, const char *key, int value) {
if (value < 0) /* undefined? */
return; /* does not set field */
lua_pushboolean(L, value);
@ -229,7 +229,7 @@ static void setboolfield (lua_State *L, const char *key, int value) {
/*
** Set all fields from structure 'tm' in the table on top of the stack
*/
static void setallfields (lua_State *L, struct tm *stm) {
static void setallfields(lua_State *L, struct tm *stm) {
setfield(L, "year", stm->tm_year, 1900);
setfield(L, "month", stm->tm_mon, 1);
setfield(L, "day", stm->tm_mday, 0);
@ -242,7 +242,7 @@ static void setallfields (lua_State *L, struct tm *stm) {
}
static int getboolfield (lua_State *L, const char *key) {
static int getboolfield(lua_State *L, const char *key) {
int res;
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
lua_pop(L, 1);
@ -250,7 +250,7 @@ static int getboolfield (lua_State *L, const char *key) {
}
static int getfield (lua_State *L, const char *key, int d, int delta) {
static int getfield(lua_State *L, const char *key, int d, int delta) {
int isnum;
int t = lua_getfield(L, -1, key); /* get field and its type */
lua_Integer res = lua_tointegerx(L, -1, &isnum);
@ -260,8 +260,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
else if (l_unlikely(d < 0)) /* absent field; no default? */
return luaL_error(L, "field '%s' missing in date table", key);
res = d;
}
else {
} else {
if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
return luaL_error(L, "field '%s' is out-of-bound", key);
res -= delta;
@ -271,7 +270,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
}
static const char *checkoption (lua_State *L, const char *conv,
static const char *checkoption(lua_State *L, const char *conv,
ptrdiff_t convlen, char *buff) {
const char *option = LUA_STRFTIMEOPTIONS;
int oplen = 1; /* length of options being checked */
@ -290,7 +289,7 @@ static const char *checkoption (lua_State *L, const char *conv,
}
static time_t l_checktime (lua_State *L, int arg) {
static time_t l_checktime(lua_State *L, int arg) {
l_timet t = l_gettime(L, arg);
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
return (time_t)t;
@ -301,7 +300,7 @@ static time_t l_checktime (lua_State *L, int arg) {
#define SIZETIMEFMT 250
static int os_date (lua_State *L) {
static int os_date(lua_State *L) {
size_t slen;
const char *s = luaL_optlstring(L, 1, "%c", &slen);
time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
@ -310,8 +309,7 @@ static int os_date (lua_State *L) {
if (*s == '!') { /* UTC? */
stm = l_gmtime(&t, &tmr);
s++; /* skip '!' */
}
else
} else
stm = l_localtime(&t, &tmr);
if (stm == NULL) /* invalid date? */
return luaL_error(L,
@ -319,8 +317,7 @@ static int os_date (lua_State *L) {
if (strcmp(s, "*t") == 0) {
lua_createtable(L, 0, 9); /* 9 = number of fields */
setallfields(L, stm);
}
else {
} else {
char cc[4]; /* buffer for individual conversion specifiers */
luaL_Buffer b;
cc[0] = '%';
@ -343,7 +340,7 @@ static int os_date (lua_State *L) {
}
static int os_time (lua_State *L) {
static int os_time(lua_State *L) {
time_t t;
if (lua_isnoneornil(L, 1)) /* called without args? */
t = time(NULL); /* get current time */
@ -369,7 +366,7 @@ static int os_time (lua_State *L) {
}
static int os_difftime (lua_State *L) {
static int os_difftime(lua_State *L) {
time_t t1 = l_checktime(L, 1);
time_t t2 = l_checktime(L, 2);
lua_pushnumber(L, (lua_Number)difftime(t1, t2));
@ -379,11 +376,13 @@ static int os_difftime (lua_State *L) {
/* }====================================================== */
static int os_setlocale (lua_State *L) {
static int os_setlocale(lua_State *L) {
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
LC_NUMERIC, LC_TIME};
LC_NUMERIC, LC_TIME
};
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
"numeric", "time", NULL};
"numeric", "time", NULL
};
const char *l = luaL_optstring(L, 1, NULL);
int op = luaL_checkoption(L, 2, "all", catnames);
lua_pushstring(L, setlocale(cat[op], l));
@ -391,7 +390,7 @@ static int os_setlocale (lua_State *L) {
}
static int os_exit (lua_State *L) {
static int os_exit(lua_State *L) {
int status;
if (lua_isboolean(L, 1))
status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
@ -423,7 +422,7 @@ static const luaL_Reg syslib[] = {
LUAMOD_API int luaopen_os (lua_State *L) {
LUAMOD_API int luaopen_os(lua_State *L) {
luaL_newlib(L, syslib);
return 1;
}

File diff suppressed because it is too large Load diff

View file

@ -163,8 +163,8 @@ typedef struct FuncState {
} FuncState;
LUAI_FUNC int luaY_nvarstack (FuncState *fs);
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
LUAI_FUNC int luaY_nvarstack(FuncState *fs);
LUAI_FUNC LClosure *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar);

View file

@ -68,7 +68,7 @@ typedef struct LG {
{ size_t t = cast_sizet(e); \
memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
static unsigned int luai_makeseed (lua_State *L) {
static unsigned int luai_makeseed(lua_State *L) {
char buff[3 * sizeof(size_t)];
unsigned int h = cast_uint(time(NULL));
int p = 0;
@ -86,7 +86,7 @@ static unsigned int luai_makeseed (lua_State *L) {
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
** invariant (and avoiding underflows in 'totalbytes')
*/
void luaE_setdebt (global_State *g, l_mem debt) {
void luaE_setdebt(global_State *g, l_mem debt) {
l_mem tb = gettotalbytes(g);
lua_assert(tb > 0);
if (debt < tb - MAX_LMEM)
@ -96,13 +96,14 @@ void luaE_setdebt (global_State *g, l_mem debt) {
}
LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
UNUSED(L); UNUSED(limit);
LUA_API int lua_setcstacklimit(lua_State *L, unsigned int limit) {
UNUSED(L);
UNUSED(limit);
return LUAI_MAXCCALLS; /* warning?? */
}
CallInfo *luaE_extendCI (lua_State *L) {
CallInfo *luaE_extendCI(lua_State *L) {
CallInfo *ci;
lua_assert(L->ci->next == NULL);
ci = luaM_new(L, CallInfo);
@ -119,7 +120,7 @@ CallInfo *luaE_extendCI (lua_State *L) {
/*
** free all CallInfo structures not in use by a thread
*/
static void freeCI (lua_State *L) {
static void freeCI(lua_State *L) {
CallInfo *ci = L->ci;
CallInfo *next = ci->next;
ci->next = NULL;
@ -135,7 +136,7 @@ static void freeCI (lua_State *L) {
** free half of the CallInfo structures not in use by a thread,
** keeping the first one.
*/
void luaE_shrinkCI (lua_State *L) {
void luaE_shrinkCI(lua_State *L) {
CallInfo *ci = L->ci->next; /* first free CallInfo */
CallInfo *next;
if (ci == NULL)
@ -162,7 +163,7 @@ void luaE_shrinkCI (lua_State *L) {
** not much larger, does not report an error (to allow overflow
** handling to work).
*/
void luaE_checkcstack (lua_State *L) {
void luaE_checkcstack(lua_State *L) {
if (getCcalls(L) == LUAI_MAXCCALLS)
luaG_runerror(L, "C stack overflow");
else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
@ -170,15 +171,16 @@ void luaE_checkcstack (lua_State *L) {
}
LUAI_FUNC void luaE_incCstack (lua_State *L) {
LUAI_FUNC void luaE_incCstack(lua_State *L) {
L->nCcalls++;
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
luaE_checkcstack(L);
}
static void stack_init (lua_State *L1, lua_State *L) {
int i; CallInfo *ci;
static void stack_init(lua_State *L1, lua_State *L) {
int i;
CallInfo *ci;
/* initialize stack array */
L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
L1->tbclist.p = L1->stack.p;
@ -200,7 +202,7 @@ static void stack_init (lua_State *L1, lua_State *L) {
}
static void freestack (lua_State *L) {
static void freestack(lua_State *L) {
if (L->stack.p == NULL)
return; /* stack not completely built yet */
L->ci = &L->base_ci; /* free the entire 'ci' list */
@ -213,7 +215,7 @@ static void freestack (lua_State *L) {
/*
** Create registry table and its predefined values
*/
static void init_registry (lua_State *L, global_State *g) {
static void init_registry(lua_State *L, global_State *g) {
/* create registry */
Table *registry = luaH_new(L);
sethvalue(L, &g->l_registry, registry);
@ -228,7 +230,7 @@ static void init_registry (lua_State *L, global_State *g) {
/*
** open parts of the state that may cause memory-allocation errors.
*/
static void f_luaopen (lua_State *L, void *ud) {
static void f_luaopen(lua_State *L, void *ud) {
global_State *g = G(L);
UNUSED(ud);
stack_init(L, L); /* init stack */
@ -246,7 +248,7 @@ static void f_luaopen (lua_State *L, void *ud) {
** preinitialize a thread with consistent values without allocating
** any memory (to avoid errors)
*/
static void preinit_thread (lua_State *L, global_State *g) {
static void preinit_thread(lua_State *L, global_State *g) {
G(L) = g;
L->stack.p = NULL;
L->ci = NULL;
@ -266,7 +268,7 @@ static void preinit_thread (lua_State *L, global_State *g) {
}
static void close_state (lua_State *L) {
static void close_state(lua_State *L) {
global_State *g = G(L);
if (!completestate(g)) /* closing a partially built state? */
luaC_freeallobjects(L); /* just collect its objects */
@ -283,7 +285,7 @@ static void close_state (lua_State *L) {
}
LUA_API lua_State *lua_newthread (lua_State *L) {
LUA_API lua_State *lua_newthread(lua_State *L) {
global_State *g = G(L);
GCObject *o;
lua_State *L1;
@ -310,7 +312,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
}
void luaE_freethread (lua_State *L, lua_State *L1) {
void luaE_freethread(lua_State *L, lua_State *L1) {
LX *l = fromstate(L1);
luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
lua_assert(L1->openupval == NULL);
@ -320,7 +322,7 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
}
int luaE_resetthread (lua_State *L, int status) {
int luaE_resetthread(lua_State *L, int status) {
CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */
setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */
ci->func.p = L->stack.p;
@ -339,7 +341,7 @@ int luaE_resetthread (lua_State *L, int status) {
}
LUA_API int lua_closethread (lua_State *L, lua_State *from) {
LUA_API int lua_closethread(lua_State *L, lua_State *from) {
int status;
lua_lock(L);
L->nCcalls = (from) ? getCcalls(from) : 0;
@ -352,12 +354,12 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) {
/*
** Deprecated! Use 'lua_closethread' instead.
*/
LUA_API int lua_resetthread (lua_State *L) {
LUA_API int lua_resetthread(lua_State *L) {
return lua_closethread(L, NULL);
}
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) {
int i;
lua_State *L;
global_State *g;
@ -403,7 +405,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->gcstepsize = LUAI_GCSTEPSIZE;
setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
g->genminormul = LUAI_GENMINORMUL;
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
for (i = 0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
/* memory allocation error: free partial state */
close_state(L);
@ -413,14 +415,14 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
}
LUA_API void lua_close (lua_State *L) {
LUA_API void lua_close(lua_State *L) {
lua_lock(L);
L = G(L)->mainthread; /* only the main thread can be closed */
close_state(L);
}
void luaE_warning (lua_State *L, const char *msg, int tocont) {
void luaE_warning(lua_State *L, const char *msg, int tocont) {
lua_WarnFunction wf = G(L)->warnf;
if (wf != NULL)
wf(G(L)->ud_warn, msg, tocont);
@ -430,7 +432,7 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) {
/*
** Generate a warning from an error message
*/
void luaE_warnerror (lua_State *L, const char *where) {
void luaE_warnerror(lua_State *L, const char *where) {
TValue *errobj = s2v(L->top.p - 1); /* error object */
const char *msg = (ttisstring(errobj))
? getstr(tsvalue(errobj))

View file

@ -393,15 +393,15 @@ union GCUnion {
/* actual number of total bytes allocated */
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
LUAI_FUNC void luaE_checkcstack (lua_State *L);
LUAI_FUNC void luaE_incCstack (lua_State *L);
LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
LUAI_FUNC int luaE_resetthread (lua_State *L, int status);
LUAI_FUNC void luaE_setdebt(global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread(lua_State *L, lua_State *L1);
LUAI_FUNC CallInfo *luaE_extendCI(lua_State *L);
LUAI_FUNC void luaE_shrinkCI(lua_State *L);
LUAI_FUNC void luaE_checkcstack(lua_State *L);
LUAI_FUNC void luaE_incCstack(lua_State *L);
LUAI_FUNC void luaE_warning(lua_State *L, const char *msg, int tocont);
LUAI_FUNC void luaE_warnerror(lua_State *L, const char *where);
LUAI_FUNC int luaE_resetthread(lua_State *L, int status);
#endif

View file

@ -31,7 +31,7 @@
/*
** equality for long strings
*/
int luaS_eqlngstr (TString *a, TString *b) {
int luaS_eqlngstr(TString *a, TString *b) {
size_t len = a->u.lnglen;
lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR);
return (a == b) || /* same instance or... */
@ -40,15 +40,15 @@ int luaS_eqlngstr (TString *a, TString *b) {
}
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed) {
unsigned int h = seed ^ cast_uint(l);
for (; l > 0; l--)
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
h ^= ((h << 5) + (h >> 2) + cast_byte(str[l - 1]));
return h;
}
unsigned int luaS_hashlongstr (TString *ts) {
unsigned int luaS_hashlongstr(TString *ts) {
lua_assert(ts->tt == LUA_VLNGSTR);
if (ts->extra == 0) { /* no hash? */
size_t len = ts->u.lnglen;
@ -59,7 +59,7 @@ unsigned int luaS_hashlongstr (TString *ts) {
}
static void tablerehash (TString **vect, int osize, int nsize) {
static void tablerehash(TString **vect, int osize, int nsize) {
int i;
for (i = osize; i < nsize; i++) /* clear new elements */
vect[i] = NULL;
@ -82,19 +82,18 @@ static void tablerehash (TString **vect, int osize, int nsize) {
** (This can degrade performance, but any non-zero size should work
** correctly.)
*/
void luaS_resize (lua_State *L, int nsize) {
void luaS_resize(lua_State *L, int nsize) {
stringtable *tb = &G(L)->strt;
int osize = tb->size;
TString **newvect;
if (nsize < osize) /* shrinking table? */
tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString *);
if (l_unlikely(newvect == NULL)) { /* reallocation failed? */
if (nsize < osize) /* was it shrinking table? */
tablerehash(tb->hash, nsize, osize); /* restore to original size */
/* leave table as it was */
}
else { /* allocation succeeded */
} else { /* allocation succeeded */
tb->hash = newvect;
tb->size = nsize;
if (nsize > osize)
@ -107,7 +106,7 @@ void luaS_resize (lua_State *L, int nsize) {
** Clear API string cache. (Entries cannot be empty, so fill them with
** a non-collectable string.)
*/
void luaS_clearcache (global_State *g) {
void luaS_clearcache(global_State *g) {
int i, j;
for (i = 0; i < STRCACHE_N; i++)
for (j = 0; j < STRCACHE_M; j++) {
@ -120,11 +119,11 @@ void luaS_clearcache (global_State *g) {
/*
** Initialize the string table and the string cache
*/
void luaS_init (lua_State *L) {
void luaS_init(lua_State *L) {
global_State *g = G(L);
int i, j;
stringtable *tb = &G(L)->strt;
tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString *);
tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
tb->size = MINSTRTABSIZE;
/* pre-create memory-error message */
@ -140,7 +139,7 @@ void luaS_init (lua_State *L) {
/*
** creates a new string object
*/
static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
static TString *createstrobj(lua_State *L, size_t l, int tag, unsigned int h) {
TString *ts;
GCObject *o;
size_t totalsize; /* total size of TString object */
@ -154,7 +153,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 *luaS_createlngstrobj(lua_State *L, size_t l) {
TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
ts->u.lnglen = l;
ts->shrlen = 0xFF; /* signals that it is a long string */
@ -162,7 +161,7 @@ TString *luaS_createlngstrobj (lua_State *L, size_t l) {
}
void luaS_remove (lua_State *L, TString *ts) {
void luaS_remove(lua_State *L, TString *ts) {
stringtable *tb = &G(L)->strt;
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
while (*p != ts) /* find previous element */
@ -172,7 +171,7 @@ void luaS_remove (lua_State *L, TString *ts) {
}
static void growstrtab (lua_State *L, stringtable *tb) {
static void growstrtab(lua_State *L, stringtable *tb) {
if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
luaC_fullgc(L, 1); /* try to free some... */
if (tb->nuse == MAX_INT) /* still too many? */
@ -186,7 +185,7 @@ static void growstrtab (lua_State *L, stringtable *tb) {
/*
** Checks whether short string exists and reuses it or creates a new one.
*/
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
static TString *internshrstr(lua_State *L, const char *str, size_t l) {
TString *ts;
global_State *g = G(L);
stringtable *tb = &g->strt;
@ -219,7 +218,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
/*
** new string (with explicit length)
*/
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
TString *luaS_newlstr(lua_State *L, const char *str, size_t l) {
if (l <= LUAI_MAXSHORTLEN) /* short string? */
return internshrstr(L, str, l);
else {
@ -239,7 +238,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
** only zero-terminated strings, so it is safe to use 'strcmp' to
** check hits.
*/
TString *luaS_new (lua_State *L, const char *str) {
TString *luaS_new(lua_State *L, const char *str) {
unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
int j;
TString **p = G(L)->strcache[i];
@ -256,7 +255,7 @@ TString *luaS_new (lua_State *L, const char *str) {
}
Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
Udata *luaS_newudata(lua_State *L, size_t s, int nuvalue) {
Udata *u;
int i;
GCObject *o;

View file

@ -41,17 +41,17 @@
#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_hashlongstr (TString *ts);
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
LUAI_FUNC void luaS_clearcache (global_State *g);
LUAI_FUNC void luaS_init (lua_State *L);
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue);
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
LUAI_FUNC unsigned int luaS_hash(const char *str, size_t l, unsigned int seed);
LUAI_FUNC unsigned int luaS_hashlongstr(TString *ts);
LUAI_FUNC int luaS_eqlngstr(TString *a, TString *b);
LUAI_FUNC void luaS_resize(lua_State *L, int newsize);
LUAI_FUNC void luaS_clearcache(global_State *g);
LUAI_FUNC void luaS_init(lua_State *L);
LUAI_FUNC void luaS_remove(lua_State *L, TString *ts);
LUAI_FUNC Udata *luaS_newudata(lua_State *L, size_t s, int nuvalue);
LUAI_FUNC TString *luaS_newlstr(lua_State *L, const char *str, size_t l);
LUAI_FUNC TString *luaS_new(lua_State *L, const char *str);
LUAI_FUNC TString *luaS_createlngstrobj(lua_State *L, size_t l);
#endif

File diff suppressed because it is too large Load diff

View file

@ -91,8 +91,9 @@
#define dummynode (&dummynode_)
static const Node dummynode_ = {
{{NULL}, LUA_VEMPTY, /* value's value and type */
LUA_VNIL, 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 */
};
@ -105,7 +106,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT};
** remainder, which is faster. Otherwise, use an unsigned-integer
** remainder, which uses all bits and ensures a non-negative result.
*/
static Node *hashint (const Table *t, lua_Integer i) {
static Node *hashint(const Table *t, lua_Integer i) {
lua_Unsigned ui = l_castS2U(i);
if (ui <= cast_uint(INT_MAX))
return hashmod(t, cast_int(ui));
@ -128,15 +129,14 @@ static Node *hashint (const Table *t, lua_Integer i) {
** INT_MIN.
*/
#if !defined(l_hashfloat)
static int l_hashfloat (lua_Number n) {
static int l_hashfloat(lua_Number n) {
int i;
lua_Integer ni;
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
return 0;
}
else { /* normal case */
} else { /* normal case */
unsigned int u = cast_uint(i) + cast_uint(ni);
return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
}
@ -148,7 +148,7 @@ static int l_hashfloat (lua_Number n) {
** returns the 'main' position of an element in a table (that is,
** the index of its hash value).
*/
static Node *mainpositionTV (const Table *t, const TValue *key) {
static Node *mainpositionTV(const Table *t, const TValue *key) {
switch (ttypetag(key)) {
case LUA_VNUMINT: {
lua_Integer i = ivalue(key);
@ -186,7 +186,7 @@ static Node *mainpositionTV (const Table *t, const TValue *key) {
}
l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) {
l_sinline Node *mainpositionfromnode(const Table *t, Node *nd) {
TValue key;
getnodekey(cast(lua_State *, NULL), &key, nd);
return mainpositionTV(t, &key);
@ -213,12 +213,14 @@ l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) {
** positive does not break anything. (In particular, 'next' will return
** some other valid item on the table or nil.)
*/
static int equalkey (const TValue *k1, const Node *n2, int deadok) {
static int equalkey(const TValue *k1, const Node *n2, int deadok) {
if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */
!(deadok && keyisdead(n2) && iscollectable(k1)))
return 0; /* cannot be same key */
switch (keytt(n2)) {
case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
case LUA_VNIL:
case LUA_VFALSE:
case LUA_VTRUE:
return 1;
case LUA_VNUMINT:
return (ivalue(k1) == keyival(n2));
@ -247,7 +249,7 @@ static int equalkey (const TValue *k1, const Node *n2, int deadok) {
/*
** Returns the real size of the 'array' array
*/
LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
LUAI_FUNC unsigned int luaH_realasize(const Table *t) {
if (limitequalsasize(t))
return t->alimit; /* this is the size */
else {
@ -264,7 +266,7 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
#endif
#endif
size++;
lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
lua_assert(ispow2(size) && size / 2 < t->alimit && t->alimit < size);
return size;
}
}
@ -275,12 +277,12 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
** (If it is not, 'alimit' cannot be changed to any other value
** without changing the real size.)
*/
static int ispow2realasize (const Table *t) {
static int ispow2realasize(const Table *t) {
return (!isrealasize(t) || ispow2(t->alimit));
}
static unsigned int setlimittosize (Table *t) {
static unsigned int setlimittosize(Table *t) {
t->alimit = luaH_realasize(t);
setrealasize(t);
return t->alimit;
@ -296,7 +298,7 @@ static unsigned int setlimittosize (Table *t) {
** which may be in array part, nor for floats with integral values.)
** See explanation about 'deadok' in function 'equalkey'.
*/
static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
static const TValue *getgeneric(Table *t, const TValue *key, int deadok) {
Node *n = mainpositionTV(t, key);
for (;;) { /* check whether 'key' is somewhere in the chain */
if (equalkey(key, n, deadok))
@ -315,7 +317,7 @@ static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
** returns the index for 'k' if 'k' is an appropriate key to live in
** the array part of a table, 0 otherwise.
*/
static unsigned int arrayindex (lua_Integer k) {
static unsigned int arrayindex(lua_Integer k) {
if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */
return cast_uint(k); /* 'key' is an appropriate array index */
else
@ -328,7 +330,7 @@ static unsigned int arrayindex (lua_Integer k) {
** elements in the array part, then elements in the hash part. The
** beginning of a traversal is signaled by 0.
*/
static unsigned int findindex (lua_State *L, Table *t, TValue *key,
static unsigned int findindex(lua_State *L, Table *t, TValue *key,
unsigned int asize) {
unsigned int i;
if (ttisnil(key)) return 0; /* first iteration */
@ -346,7 +348,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key,
}
int luaH_next (lua_State *L, Table *t, StkId key) {
int luaH_next(lua_State *L, Table *t, StkId key) {
unsigned int asize = luaH_realasize(t);
unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
for (; i < asize; i++) { /* try first array part */
@ -368,7 +370,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
}
static void freehash (lua_State *L, Table *t) {
static void freehash(lua_State *L, Table *t) {
if (!isdummy(t))
luaM_freearray(L, t->node, cast_sizet(sizenode(t)));
}
@ -388,7 +390,7 @@ static void freehash (lua_State *L, Table *t) {
** will go to the array part; return the optimal size. (The condition
** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.)
*/
static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
static unsigned int computesizes(unsigned int nums[], unsigned int *pna) {
int i;
unsigned int twotoi; /* 2^i (candidate for optimal size) */
unsigned int a = 0; /* number of elements smaller than 2^i */
@ -399,7 +401,7 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
twotoi > 0 && *pna > twotoi / 2;
i++, twotoi *= 2) {
a += nums[i];
if (a > twotoi/2) { /* more than half elements present? */
if (a > twotoi / 2) { /* more than half elements present? */
optimal = twotoi; /* optimal size (till now) */
na = a; /* all elements up to 'optimal' will go to array part */
}
@ -410,13 +412,12 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
}
static int countint (lua_Integer key, unsigned int *nums) {
static int countint(lua_Integer key, unsigned int *nums) {
unsigned int k = arrayindex(key);
if (k != 0) { /* is 'key' an appropriate array index? */
nums[luaO_ceillog2(k)]++; /* count as such */
return 1;
}
else
} else
return 0;
}
@ -426,7 +427,7 @@ static int countint (lua_Integer key, unsigned int *nums) {
** number of keys that will go into corresponding slice and return
** total number of non-nil keys.
*/
static unsigned int numusearray (const Table *t, unsigned int *nums) {
static unsigned int numusearray(const Table *t, unsigned int *nums) {
int lg;
unsigned int ttlg; /* 2^lg */
unsigned int ause = 0; /* summation of 'nums' */
@ -443,7 +444,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) {
}
/* count elements in range (2^(lg - 1), 2^lg] */
for (; i <= lim; i++) {
if (!isempty(&t->array[i-1]))
if (!isempty(&t->array[i - 1]))
lc++;
}
nums[lg] += lc;
@ -453,7 +454,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) {
}
static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
static int numusehash(const Table *t, unsigned int *nums, unsigned int *pna) {
int totaluse = 0; /* total number of elements */
int ause = 0; /* elements added to 'nums' (can go to array part) */
int i = sizenode(t);
@ -477,13 +478,12 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
** comparison ensures that the shift in the second one does not
** overflow.
*/
static void setnodevector (lua_State *L, Table *t, unsigned int size) {
static void setnodevector(lua_State *L, Table *t, unsigned int size) {
if (size == 0) { /* no elements to hash part? */
t->node = cast(Node *, dummynode); /* use common 'dummynode' */
t->lsizenode = 0;
t->lastfree = NULL; /* signal that it is using dummy node */
}
else {
} else {
int i;
int lsize = luaO_ceillog2(size);
if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
@ -505,7 +505,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) {
/*
** (Re)insert all elements from the hash part of 'ot' into table 't'.
*/
static void reinsert (lua_State *L, Table *ot, Table *t) {
static void reinsert(lua_State *L, Table *ot, Table *t) {
int j;
int size = sizenode(ot);
for (j = 0; j < size; j++) {
@ -524,7 +524,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) {
/*
** Exchange the hash part of 't1' and 't2'.
*/
static void exchangehashpart (Table *t1, Table *t2) {
static void exchangehashpart(Table *t1, Table *t2) {
lu_byte lsizenode = t1->lsizenode;
Node *node = t1->node;
Node *lastfree = t1->lastfree;
@ -550,7 +550,7 @@ static void exchangehashpart (Table *t1, Table *t2) {
** nils and reinserts the elements of the old hash back into the new
** parts of the table.
*/
void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
void luaH_resize(lua_State *L, Table *t, unsigned int newasize,
unsigned int nhsize) {
unsigned int i;
Table newt; /* to keep the new hash part */
@ -587,7 +587,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
}
void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
void luaH_resizearray(lua_State *L, Table *t, unsigned int nasize) {
int nsize = allocsizenode(t);
luaH_resize(L, t, nasize, nsize);
}
@ -595,7 +595,7 @@ void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
/*
** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
*/
static void rehash (lua_State *L, Table *t, const TValue *ek) {
static void rehash(lua_State *L, Table *t, const TValue *ek) {
unsigned int asize; /* optimal size for array part */
unsigned int na; /* number of keys in the array part */
unsigned int nums[MAXABITS + 1];
@ -623,7 +623,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
*/
Table *luaH_new (lua_State *L) {
Table *luaH_new(lua_State *L) {
GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
Table *t = gco2t(o);
t->metatable = NULL;
@ -635,14 +635,14 @@ Table *luaH_new (lua_State *L) {
}
void luaH_free (lua_State *L, Table *t) {
void luaH_free(lua_State *L, Table *t) {
freehash(L, t);
luaM_freearray(L, t->array, luaH_realasize(t));
luaM_free(L, t);
}
static Node *getfreepos (Table *t) {
static Node *getfreepos(Table *t) {
if (!isdummy(t)) {
while (t->lastfree > t->node) {
t->lastfree--;
@ -662,7 +662,7 @@ static Node *getfreepos (Table *t) {
** put new key in its main position; otherwise (colliding node is in its main
** position), new key goes to an empty position.
*/
static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
static void luaH_newkey(lua_State *L, Table *t, const TValue *key,
TValue *value) {
Node *mp;
TValue aux;
@ -674,8 +674,7 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
setivalue(&aux, k);
key = &aux; /* insert it as an integer */
}
else if (l_unlikely(luai_numisnan(f)))
} else if (l_unlikely(luai_numisnan(f)))
luaG_runerror(L, "table index is NaN");
}
if (ttisnil(value))
@ -703,8 +702,7 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
gnext(mp) = 0; /* now 'mp' is free */
}
setempty(gval(mp));
}
else { /* colliding node is in its own main position */
} else { /* colliding node is in its own main position */
/* new node will go into free position */
if (gnext(mp) != 0)
gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
@ -742,7 +740,7 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
** If key is 0 or negative, 'res' will have its higher bit on, so that
** if cannot be smaller than alimit.
*/
const TValue *luaH_getint (Table *t, lua_Integer key) {
const TValue *luaH_getint(Table *t, lua_Integer key) {
lua_Unsigned alimit = t->alimit;
if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */
return &t->array[key - 1];
@ -750,8 +748,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
(((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) {
t->alimit = cast_uint(key); /* probably '#t' is here now */
return &t->array[key - 1];
}
else { /* key is not in the array part; check the hash */
} else { /* key is not in the array part; check the hash */
Node *n = hashint(t, key);
for (;;) { /* check whether 'key' is somewhere in the chain */
if (keyisinteger(n) && keyival(n) == key)
@ -770,7 +767,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
/*
** search function for short strings
*/
const TValue *luaH_getshortstr (Table *t, TString *key) {
const TValue *luaH_getshortstr(Table *t, TString *key) {
Node *n = hashstr(t, key);
lua_assert(key->tt == LUA_VSHRSTR);
for (;;) { /* check whether 'key' is somewhere in the chain */
@ -786,7 +783,7 @@ const TValue *luaH_getshortstr (Table *t, TString *key) {
}
const TValue *luaH_getstr (Table *t, TString *key) {
const TValue *luaH_getstr(Table *t, TString *key) {
if (key->tt == LUA_VSHRSTR)
return luaH_getshortstr(t, key);
else { /* for long strings, use generic case */
@ -800,11 +797,14 @@ const TValue *luaH_getstr (Table *t, TString *key) {
/*
** main search function
*/
const TValue *luaH_get (Table *t, const TValue *key) {
const TValue *luaH_get(Table *t, const TValue *key) {
switch (ttypetag(key)) {
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_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, F2Ieq)) /* integral index? */
@ -823,7 +823,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
** Beware: when using this function you probably need to check a GC
** barrier and invalidate the TM cache.
*/
void luaH_finishset (lua_State *L, Table *t, const TValue *key,
void luaH_finishset(lua_State *L, Table *t, const TValue *key,
const TValue *slot, TValue *value) {
if (isabstkey(slot))
luaH_newkey(L, t, key, value);
@ -836,20 +836,19 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key,
** beware: when using this function you probably need to check a GC
** barrier and invalidate the TM cache.
*/
void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) {
void luaH_set(lua_State *L, Table *t, const TValue *key, TValue *value) {
const TValue *slot = luaH_get(t, key);
luaH_finishset(L, t, key, slot, value);
}
void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
void luaH_setint(lua_State *L, Table *t, lua_Integer key, TValue *value) {
const TValue *p = luaH_getint(t, key);
if (isabstkey(p)) {
TValue k;
setivalue(&k, key);
luaH_newkey(L, t, &k, value);
}
else
} else
setobj2t(L, cast(TValue *, p), value);
}
@ -867,7 +866,7 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
** boundary. ('j + 1' cannot be a present integer key because it is
** not a valid integer in Lua.)
*/
static lua_Unsigned hash_search (Table *t, lua_Unsigned j) {
static lua_Unsigned hash_search(Table *t, lua_Unsigned j) {
lua_Unsigned i;
if (j == 0) j++; /* the caller ensures 'j + 1' is present */
do {
@ -892,7 +891,7 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) {
}
static unsigned int binsearch (const TValue *array, unsigned int i,
static unsigned int binsearch(const TValue *array, unsigned int i,
unsigned int j) {
while (j - i > 1u) { /* binary search */
unsigned int m = (i + j) / 2;
@ -935,7 +934,7 @@ static unsigned int binsearch (const TValue *array, unsigned int i,
** (In those cases, the boundary is not inside the array part, and
** therefore cannot be used as a new limit.)
*/
lua_Unsigned luaH_getn (Table *t) {
lua_Unsigned luaH_getn(Table *t) {
unsigned int limit = t->alimit;
if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
/* there must be a boundary before 'limit' */
@ -946,8 +945,7 @@ lua_Unsigned luaH_getn (Table *t) {
setnorealasize(t); /* now 'alimit' is not the real size */
}
return limit - 1;
}
else { /* must search for a boundary in [0, limit] */
} else { /* must search for a boundary in [0, limit] */
unsigned int boundary = binsearch(t->array, 0, limit);
/* can this boundary represent the real size of the array? */
if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
@ -988,7 +986,7 @@ lua_Unsigned luaH_getn (Table *t) {
/* export these functions for the test library */
Node *luaH_mainposition (const Table *t, const TValue *key) {
Node *luaH_mainposition(const Table *t, const TValue *key) {
return mainpositionTV(t, key);
}

View file

@ -35,28 +35,28 @@
#define nodefromval(v) cast(Node *, (v))
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
LUAI_FUNC const TValue *luaH_getint(Table *t, lua_Integer key);
LUAI_FUNC void luaH_setint(lua_State *L, Table *t, lua_Integer key,
TValue *value);
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key,
LUAI_FUNC const TValue *luaH_getshortstr(Table *t, TString *key);
LUAI_FUNC const TValue *luaH_getstr(Table *t, TString *key);
LUAI_FUNC const TValue *luaH_get(Table *t, const TValue *key);
LUAI_FUNC void luaH_set(lua_State *L, Table *t, const TValue *key,
TValue *value);
LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key,
LUAI_FUNC void luaH_finishset(lua_State *L, Table *t, const TValue *key,
const TValue *slot, TValue *value);
LUAI_FUNC Table *luaH_new (lua_State *L);
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
LUAI_FUNC Table *luaH_new(lua_State *L);
LUAI_FUNC void luaH_resize(lua_State *L, Table *t, unsigned int nasize,
unsigned int nhsize);
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
LUAI_FUNC lua_Unsigned luaH_getn (Table *t);
LUAI_FUNC unsigned int luaH_realasize (const Table *t);
LUAI_FUNC void luaH_resizearray(lua_State *L, Table *t, unsigned int nasize);
LUAI_FUNC void luaH_free(lua_State *L, Table *t);
LUAI_FUNC int luaH_next(lua_State *L, Table *t, StkId key);
LUAI_FUNC lua_Unsigned luaH_getn(Table *t);
LUAI_FUNC unsigned int luaH_realasize(const Table *t);
#if defined(LUA_DEBUG)
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
LUAI_FUNC Node *luaH_mainposition(const Table *t, const TValue *key);
#endif

View file

@ -33,7 +33,7 @@
#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
static int checkfield (lua_State *L, const char *key, int n) {
static int checkfield(lua_State *L, const char *key, int n) {
lua_pushstring(L, key);
return (lua_rawget(L, -n) != LUA_TNIL);
}
@ -43,7 +43,7 @@ static int checkfield (lua_State *L, const char *key, int n) {
** Check that 'arg' either is a table or can behave like one (that is,
** has a metatable with the required metamethods)
*/
static void checktab (lua_State *L, int arg, int what) {
static void checktab(lua_State *L, int arg, int what) {
if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
int n = 1; /* number of elements to pop */
if (lua_getmetatable(L, arg) && /* must have metatable */
@ -51,14 +51,13 @@ static void checktab (lua_State *L, int arg, int what) {
(!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
(!(what & TAB_L) || checkfield(L, "__len", ++n))) {
lua_pop(L, n); /* pop metatable and tested metamethods */
}
else
} else
luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
}
}
static int tinsert (lua_State *L) {
static int tinsert(lua_State *L) {
lua_Integer pos; /* where to insert new element */
lua_Integer e = aux_getn(L, 1, TAB_RW);
e = luaL_intop(+, e, 1); /* first empty element */
@ -88,7 +87,7 @@ static int tinsert (lua_State *L) {
}
static int tremove (lua_State *L) {
static int tremove(lua_State *L) {
lua_Integer size = aux_getn(L, 1, TAB_RW);
lua_Integer pos = luaL_optinteger(L, 2, size);
if (pos != size) /* validate 'pos' if given */
@ -96,7 +95,7 @@ static int tremove (lua_State *L) {
luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
"position out of bounds");
lua_geti(L, 1, pos); /* result = t[pos] */
for ( ; pos < size; pos++) {
for (; pos < size; pos++) {
lua_geti(L, 1, pos + 1);
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
}
@ -112,7 +111,7 @@ static int tremove (lua_State *L) {
** "possible" means destination after original range, or smaller
** than origin, or copying to another table.
*/
static int tmove (lua_State *L) {
static int tmove(lua_State *L) {
lua_Integer f = luaL_checkinteger(L, 2);
lua_Integer e = luaL_checkinteger(L, 3);
lua_Integer t = luaL_checkinteger(L, 4);
@ -131,8 +130,7 @@ static int tmove (lua_State *L) {
lua_geti(L, 1, f + i);
lua_seti(L, tt, t + i);
}
}
else {
} else {
for (i = n - 1; i >= 0; i--) {
lua_geti(L, 1, f + i);
lua_seti(L, tt, t + i);
@ -144,7 +142,7 @@ static int tmove (lua_State *L) {
}
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
static void addfield(lua_State *L, luaL_Buffer *b, lua_Integer i) {
lua_geti(L, 1, i);
if (l_unlikely(!lua_isstring(L, -1)))
luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
@ -153,7 +151,7 @@ static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
}
static int tconcat (lua_State *L) {
static int tconcat(lua_State *L) {
luaL_Buffer b;
lua_Integer last = aux_getn(L, 1, TAB_R);
size_t lsep;
@ -178,7 +176,7 @@ static int tconcat (lua_State *L) {
** =======================================================
*/
static int tpack (lua_State *L) {
static int tpack(lua_State *L) {
int i;
int n = lua_gettop(L); /* number of elements to pack */
lua_createtable(L, n, 1); /* create result table */
@ -191,7 +189,7 @@ static int tpack (lua_State *L) {
}
static int tunpack (lua_State *L) {
static int tunpack(lua_State *L) {
lua_Unsigned n;
lua_Integer i = luaL_optinteger(L, 2, 1);
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
@ -243,7 +241,7 @@ typedef unsigned int IdxT;
** anything without risking overflows. A safe way to use their values
** is to copy them to an array of a known type and use the array values.
*/
static unsigned int l_randomizePivot (void) {
static unsigned int l_randomizePivot(void) {
clock_t c = clock();
time_t t = time(NULL);
unsigned int buff[sof(c) + sof(t)];
@ -262,7 +260,7 @@ static unsigned int l_randomizePivot (void) {
#define RANLIMIT 100u
static void set2 (lua_State *L, IdxT i, IdxT j) {
static void set2(lua_State *L, IdxT i, IdxT j) {
lua_seti(L, 1, i);
lua_seti(L, 1, j);
}
@ -272,14 +270,14 @@ static void set2 (lua_State *L, IdxT i, IdxT j) {
** Return true iff value at stack index 'a' is less than the value at
** index 'b' (according to the order of the sort).
*/
static int sort_comp (lua_State *L, int a, int b) {
static int sort_comp(lua_State *L, int a, int b) {
if (lua_isnil(L, 2)) /* no function? */
return lua_compare(L, a, b, LUA_OPLT); /* a < b */
else { /* function */
int res;
lua_pushvalue(L, 2); /* push function */
lua_pushvalue(L, a-1); /* -1 to compensate function */
lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
lua_pushvalue(L, a - 1); /* -1 to compensate function */
lua_pushvalue(L, b - 2); /* -2 to compensate function and 'a' */
lua_call(L, 2, 1); /* call function */
res = lua_toboolean(L, -1); /* get result */
lua_pop(L, 1); /* pop result */
@ -295,7 +293,7 @@ static int sort_comp (lua_State *L, int a, int b) {
** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
** returns 'i'.
*/
static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
static IdxT partition(lua_State *L, IdxT lo, IdxT up) {
IdxT i = lo; /* will be incremented before first use */
IdxT j = up - 1; /* will be decremented before first use */
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
@ -331,7 +329,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
** Choose an element in the middle (2nd-3th quarters) of [lo,up]
** "randomized" by 'rnd'
*/
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
static IdxT choosePivot(IdxT lo, IdxT up, unsigned int rnd) {
IdxT r4 = (up - lo) / 4; /* range/4 */
IdxT p = rnd % (r4 * 2) + (lo + r4);
lua_assert(lo + r4 <= p && p <= up - r4);
@ -342,7 +340,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
/*
** Quicksort algorithm (recursive function)
*/
static void auxsort (lua_State *L, IdxT lo, IdxT up,
static void auxsort(lua_State *L, IdxT lo, IdxT up,
unsigned int rnd) {
while (lo < up) { /* loop for tail recursion */
IdxT p; /* Pivot index */
@ -357,7 +355,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up,
if (up - lo == 1) /* only 2 elements? */
return; /* already sorted */
if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
p = (lo + up)/2; /* middle element is a good pivot */
p = (lo + up) / 2; /* middle element is a good pivot */
else /* for larger intervals, it is worth a random pivot */
p = choosePivot(lo, up, rnd);
lua_geti(L, 1, p);
@ -384,8 +382,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up,
auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
n = p - lo; /* size of smaller interval */
lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
}
else {
} else {
auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
n = up - p; /* size of smaller interval */
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
@ -396,7 +393,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up,
}
static int sort (lua_State *L) {
static int sort(lua_State *L) {
lua_Integer n = aux_getn(L, 1, TAB_RW);
if (n > 1) { /* non-trivial interval? */
luaL_argcheck(L, n < INT_MAX, 1, "array too big");
@ -423,7 +420,7 @@ static const luaL_Reg tab_funcs[] = {
};
LUAMOD_API int luaopen_table (lua_State *L) {
LUAMOD_API int luaopen_table(lua_State *L) {
luaL_newlib(L, tab_funcs);
return 1;
}

View file

@ -35,7 +35,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
};
void luaT_init (lua_State *L) {
void luaT_init(lua_State *L) {
static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex",
"__gc", "__mode", "__len", "__eq",
@ -46,7 +46,7 @@ void luaT_init (lua_State *L) {
"__concat", "__call", "__close"
};
int i;
for (i=0; i<TM_N; i++) {
for (i = 0; i < TM_N; i++) {
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
}
@ -57,18 +57,17 @@ void luaT_init (lua_State *L) {
** function to be used with macro "fasttm": optimized for absence of
** tag methods
*/
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
const TValue *luaT_gettm(Table *events, TMS event, TString *ename) {
const TValue *tm = luaH_getshortstr(events, ename);
lua_assert(event <= TM_EQ);
if (notm(tm)) { /* no tag method? */
events->flags |= cast_byte(1u<<event); /* cache this fact */
events->flags |= cast_byte(1u << event); /* cache this fact */
return NULL;
}
else return tm;
} else return tm;
}
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event) {
Table *mt;
switch (ttype(o)) {
case LUA_TTABLE:
@ -88,7 +87,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
** Return the name of the type of an object. For tables and userdata
** with metatable, use their '__name' metafield, if present.
*/
const char *luaT_objtypename (lua_State *L, const TValue *o) {
const char *luaT_objtypename(lua_State *L, const TValue *o) {
Table *mt;
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
@ -100,7 +99,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
}
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3) {
StkId func = L->top.p;
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
@ -116,7 +115,7 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
}
void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
void luaT_callTMres(lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, StkId res) {
ptrdiff_t result = savestack(L, res);
StkId func = L->top.p;
@ -134,7 +133,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
}
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
static int callbinTM(lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
if (notm(tm))
@ -145,12 +144,16 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
}
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event) {
if (l_unlikely(!callbinTM(L, p1, p2, res, event))) {
switch (event) {
case TM_BAND: case TM_BOR: case TM_BXOR:
case TM_SHL: case TM_SHR: case TM_BNOT: {
case TM_BAND:
case TM_BOR:
case TM_BXOR:
case TM_SHL:
case TM_SHR:
case TM_BNOT: {
if (ttisnumber(p1) && ttisnumber(p2))
luaG_tointerror(L, p1, p2);
else
@ -164,7 +167,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
}
void luaT_tryconcatTM (lua_State *L) {
void luaT_tryconcatTM(lua_State *L) {
StkId top = L->top.p;
if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2,
TM_CONCAT)))
@ -172,7 +175,7 @@ void luaT_tryconcatTM (lua_State *L) {
}
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
void luaT_trybinassocTM(lua_State *L, const TValue *p1, const TValue *p2,
int flip, StkId res, TMS event) {
if (flip)
luaT_trybinTM(L, p2, p1, res, event);
@ -181,7 +184,7 @@ void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
}
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
void luaT_trybiniTM(lua_State *L, const TValue *p1, lua_Integer i2,
int flip, StkId res, TMS event) {
TValue aux;
setivalue(&aux, i2);
@ -198,7 +201,7 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
** 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,
int luaT_callorderTM(lua_State *L, const TValue *p1, const TValue *p2,
TMS event) {
if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */
return !l_isfalse(s2v(L->top.p));
@ -218,24 +221,24 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
}
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
int luaT_callorderiTM(lua_State *L, const TValue *p1, int v2,
int flip, int isfloat, TMS event) {
TValue aux; const TValue *p2;
TValue aux;
const TValue *p2;
if (isfloat) {
setfltvalue(&aux, cast_num(v2));
}
else
} else
setivalue(&aux, v2);
if (flip) { /* arguments were exchanged? */
p2 = p1; p1 = &aux; /* correct them */
}
else
p2 = p1;
p1 = &aux; /* correct them */
} else
p2 = &aux;
return luaT_callorderTM(L, p1, p2, event);
}
void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
void luaT_adjustvarargs(lua_State *L, int nfixparams, CallInfo *ci,
const Proto *p) {
int i;
int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */
@ -255,7 +258,7 @@ void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci,
}
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
void luaT_getvarargs(lua_State *L, CallInfo *ci, StkId where, int wanted) {
int i;
int nextra = ci->u.l.nextraargs;
if (wanted < 0) {

View file

@ -71,32 +71,32 @@ typedef enum {
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];)
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
LUAI_FUNC const char *luaT_objtypename(lua_State *L, const TValue *o);
LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
LUAI_FUNC const TValue *luaT_gettm(Table *events, TMS event, TString *ename);
LUAI_FUNC const TValue *luaT_gettmbyobj(lua_State *L, const TValue *o,
TMS event);
LUAI_FUNC void luaT_init (lua_State *L);
LUAI_FUNC void luaT_init(lua_State *L);
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
LUAI_FUNC void luaT_callTM(lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3);
LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f,
LUAI_FUNC void luaT_callTMres(lua_State *L, const TValue *f,
const TValue *p1, const TValue *p2, StkId p3);
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
LUAI_FUNC void luaT_trybinTM(lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event);
LUAI_FUNC void luaT_tryconcatTM (lua_State *L);
LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1,
LUAI_FUNC void luaT_tryconcatTM(lua_State *L);
LUAI_FUNC void luaT_trybinassocTM(lua_State *L, const TValue *p1,
const TValue *p2, int inv, StkId res, TMS event);
LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
LUAI_FUNC void luaT_trybiniTM(lua_State *L, const TValue *p1, lua_Integer i2,
int inv, StkId res, TMS event);
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
LUAI_FUNC int luaT_callorderTM(lua_State *L, const TValue *p1,
const TValue *p2, TMS event);
LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
LUAI_FUNC int luaT_callorderiTM(lua_State *L, const TValue *p1, int v2,
int inv, int isfloat, TMS event);
LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams,
LUAI_FUNC void luaT_adjustvarargs(lua_State *L, int nfixparams,
struct CallInfo *ci, const Proto *p);
LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci,
LUAI_FUNC void luaT_getvarargs(lua_State *L, struct CallInfo *ci,
StkId where, int wanted);

View file

@ -42,7 +42,7 @@ static const char *progname = LUA_PROGNAME;
/*
** Use 'sigaction' when available.
*/
static void setsignal (int sig, void (*handler)(int)) {
static void setsignal(int sig, void (*handler)(int)) {
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
@ -60,7 +60,7 @@ static void setsignal (int sig, void (*handler)(int)) {
/*
** Hook set by signal function to stop the interpreter.
*/
static void lstop (lua_State *L, lua_Debug *ar) {
static void lstop(lua_State *L, lua_Debug *ar) {
(void)ar; /* unused arg. */
lua_sethook(L, NULL, 0, 0); /* reset hook */
luaL_error(L, "interrupted!");
@ -73,14 +73,14 @@ static void lstop (lua_State *L, lua_Debug *ar) {
** this function only sets a hook that, when called, will stop the
** interpreter.
*/
static void laction (int i) {
static void laction(int i) {
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
lua_sethook(globalL, lstop, flag, 1);
}
static void print_usage (const char *badoption) {
static void print_usage(const char *badoption) {
lua_writestringerror("%s: ", progname);
if (badoption[1] == 'e' || badoption[1] == 'l')
lua_writestringerror("'%s' needs argument\n", badoption);
@ -107,7 +107,7 @@ static void print_usage (const char *badoption) {
** Prints an error message, adding the program name in front of it
** (if present)
*/
static void l_message (const char *pname, const char *msg) {
static void l_message(const char *pname, const char *msg) {
if (pname) lua_writestringerror("%s: ", pname);
lua_writestringerror("%s\n", msg);
}
@ -117,7 +117,7 @@ static void l_message (const char *pname, const char *msg) {
** Check whether 'status' is not OK and, if so, prints the error
** message on the top of the stack.
*/
static int report (lua_State *L, int status) {
static int report(lua_State *L, int status) {
if (status != LUA_OK) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL)
@ -132,7 +132,7 @@ static int report (lua_State *L, int status) {
/*
** Message handler used to run all chunks
*/
static int msghandler (lua_State *L) {
static int msghandler(lua_State *L) {
const char *msg = lua_tostring(L, 1);
if (msg == NULL) { /* is error object not a string? */
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
@ -151,7 +151,7 @@ static int msghandler (lua_State *L) {
** Interface to 'lua_pcall', which sets appropriate message function
** and C-signal handler. Used to run all chunks.
*/
static int docall (lua_State *L, int narg, int nres) {
static int docall(lua_State *L, int narg, int nres) {
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, msghandler); /* push message handler */
@ -165,7 +165,7 @@ static int docall (lua_State *L, int narg, int nres) {
}
static void print_version (void) {
static void print_version(void) {
lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
lua_writeline();
}
@ -181,7 +181,7 @@ static void print_version (void) {
** (If there is no interpreter's name either, 'script' is -1, so
** table sizes are zero.)
*/
static void createargtable (lua_State *L, char **argv, int argc, int script) {
static void createargtable(lua_State *L, char **argv, int argc, int script) {
int i, narg;
narg = argc - (script + 1); /* number of positive indices */
lua_createtable(L, narg, script + 1);
@ -193,18 +193,18 @@ static void createargtable (lua_State *L, char **argv, int argc, int script) {
}
static int dochunk (lua_State *L, int status) {
static int dochunk(lua_State *L, int status) {
if (status == LUA_OK) status = docall(L, 0, 0);
return report(L, status);
}
static int dofile (lua_State *L, const char *name) {
static int dofile(lua_State *L, const char *name) {
return dochunk(L, luaL_loadfile(L, name));
}
static int dostring (lua_State *L, const char *s, const char *name) {
static int dostring(lua_State *L, const char *s, const char *name) {
return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
}
@ -214,15 +214,14 @@ static int dostring (lua_State *L, const char *s, const char *name) {
** If there is no explicit modname and globname contains a '-', cut
** the suffix after '-' (the "version") to make the global name.
*/
static int dolibrary (lua_State *L, char *globname) {
static int dolibrary(lua_State *L, char *globname) {
int status;
char *suffix = NULL;
char *modname = strchr(globname, '=');
if (modname == NULL) { /* no explicit name? */
modname = globname; /* module name is equal to global name */
suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */
}
else {
} else {
*modname = '\0'; /* global name ends here */
modname++; /* module name starts after the '=' */
}
@ -241,7 +240,7 @@ static int dolibrary (lua_State *L, char *globname) {
/*
** Push on the stack the contents of table 'arg' from 1 to #arg
*/
static int pushargs (lua_State *L) {
static int pushargs(lua_State *L) {
int i, n;
if (lua_getglobal(L, "arg") != LUA_TTABLE)
luaL_error(L, "'arg' is not a table");
@ -254,7 +253,7 @@ static int pushargs (lua_State *L) {
}
static int handle_script (lua_State *L, char **argv) {
static int handle_script(lua_State *L, char **argv) {
int status;
const char *fname = argv[0];
if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
@ -283,14 +282,13 @@ static int handle_script (lua_State *L, char **argv) {
** argument. Otherwise, 'first' is -1 if there is no program name,
** 0 if there is no script name, or the index of the script name.
*/
static int collectargs (char **argv, int *first) {
static int collectargs(char **argv, int *first) {
int args = 0;
int i;
if (argv[0] != NULL) { /* is there a program name? */
if (argv[0][0]) /* not empty? */
progname = argv[0]; /* save it */
}
else { /* no program name */
} else { /* no program name */
*first = -1;
return 0;
}
@ -345,13 +343,14 @@ static int collectargs (char **argv, int *first) {
** 'W', which also affects the state.
** Returns 0 if some code raises an error.
*/
static int runargs (lua_State *L, char **argv, int n) {
static int runargs(lua_State *L, char **argv, int n) {
int i;
for (i = 1; i < n; i++) {
int option = argv[i][1];
lua_assert(argv[i][0] == '-'); /* already checked */
switch (option) {
case 'e': case 'l': {
case 'e':
case 'l': {
int status;
char *extra = argv[i] + 2; /* both options need an argument */
if (*extra == '\0') extra = argv[++i];
@ -371,7 +370,7 @@ static int runargs (lua_State *L, char **argv, int n) {
}
static int handle_luainit (lua_State *L) {
static int handle_luainit(lua_State *L) {
const char *name = "=" LUA_INITVARVERSION;
const char *init = getenv(name + 1);
if (init == NULL) {
@ -380,7 +379,7 @@ static int handle_luainit (lua_State *L) {
}
if (init == NULL) return LUA_OK;
else if (init[0] == '@')
return dofile(L, init+1);
return dofile(L, init + 1);
else
return dostring(L, init, name);
}
@ -466,7 +465,7 @@ static int handle_luainit (lua_State *L) {
** the string (or nil, if using the default value) on the stack, to keep
** it anchored.
*/
static const char *get_prompt (lua_State *L, int firstline) {
static const char *get_prompt(lua_State *L, int firstline) {
if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
else { /* apply 'tostring' over the value */
@ -486,7 +485,7 @@ static const char *get_prompt (lua_State *L, int firstline) {
** message at the top of the stack ends with the above mark for
** incomplete statements.
*/
static int incomplete (lua_State *L, int status) {
static int incomplete(lua_State *L, int status) {
if (status == LUA_ERRSYNTAX) {
size_t lmsg;
const char *msg = lua_tolstring(L, -1, &lmsg);
@ -502,7 +501,7 @@ static int incomplete (lua_State *L, int status) {
/*
** Prompt the user, read a line, and push it into the Lua stack.
*/
static int pushline (lua_State *L, int firstline) {
static int pushline(lua_State *L, int firstline) {
char buffer[LUA_MAXINPUT];
char *b = buffer;
size_t l;
@ -512,7 +511,7 @@ static int pushline (lua_State *L, int firstline) {
return 0; /* no input (prompt will be popped by caller) */
lua_pop(L, 1); /* remove prompt */
l = strlen(b);
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
if (l > 0 && b[l - 1] == '\n') /* line ends with newline? */
b[--l] = '\0'; /* remove it */
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
@ -527,7 +526,7 @@ static int pushline (lua_State *L, int firstline) {
** Try to compile line on the stack as 'return <line>;'; on return, stack
** has either compiled chunk or original line (if compilation failed).
*/
static int addreturn (lua_State *L) {
static int addreturn(lua_State *L) {
const char *line = lua_tostring(L, -1); /* original line */
const char *retline = lua_pushfstring(L, "return %s;", line);
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
@ -535,8 +534,7 @@ static int addreturn (lua_State *L) {
lua_remove(L, -2); /* remove modified line */
if (line[0] != '\0') /* non empty? */
lua_saveline(L, line); /* keep history */
}
else
} else
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
return status;
}
@ -545,7 +543,7 @@ static int addreturn (lua_State *L) {
/*
** Read multiple lines until a complete Lua statement
*/
static int multiline (lua_State *L) {
static int multiline(lua_State *L) {
for (;;) { /* repeat until gets a complete statement */
size_t len;
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
@ -567,7 +565,7 @@ static int multiline (lua_State *L) {
** the final status of load/call with the resulting function (if any)
** in the top of the stack.
*/
static int loadline (lua_State *L) {
static int loadline(lua_State *L) {
int status;
lua_settop(L, 0);
if (!pushline(L, 1))
@ -583,7 +581,7 @@ static int loadline (lua_State *L) {
/*
** Prints (calling the Lua 'print' function) any values on the stack
*/
static void l_print (lua_State *L) {
static void l_print(lua_State *L) {
int n = lua_gettop(L);
if (n > 0) { /* any result to be printed? */
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
@ -600,7 +598,7 @@ static void l_print (lua_State *L) {
** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
** print any results.
*/
static void doREPL (lua_State *L) {
static void doREPL(lua_State *L) {
int status;
const char *oldprogname = progname;
progname = NULL; /* no 'progname' on errors in interactive mode */
@ -623,7 +621,7 @@ static void doREPL (lua_State *L) {
** Main body of stand-alone interpreter (to be called in protected mode).
** Reads the options and handles them all.
*/
static int pmain (lua_State *L) {
static int pmain(lua_State *L) {
int argc = (int)lua_tointeger(L, 1);
char **argv = (char **)lua_touserdata(L, 2);
int script;
@ -660,15 +658,14 @@ static int pmain (lua_State *L) {
if (lua_stdin_is_tty()) { /* running in interactive mode? */
print_version();
doREPL(L); /* do read-eval-print loop */
}
else dofile(L, NULL); /* executes stdin as a file */
} else dofile(L, NULL); /* executes stdin as a file */
}
lua_pushboolean(L, 1); /* signal no errors */
return 1;
}
int main (int argc, char **argv) {
int main(int argc, char **argv) {
int status, result;
lua_State *L = luaL_newstate(); /* create state */
if (L == NULL) {

View file

@ -103,32 +103,32 @@ typedef LUA_KCONTEXT lua_KContext;
/*
** Type for C functions registered with Lua
*/
typedef int (*lua_CFunction) (lua_State *L);
typedef int (*lua_CFunction)(lua_State *L);
/*
** Type for continuation functions
*/
typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
/*
** Type for functions that read/write blocks when loading/dumping Lua chunks
*/
typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
typedef const char *(*lua_Reader)(lua_State *L, void *ud, size_t *sz);
typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
typedef int (*lua_Writer)(lua_State *L, const void *p, size_t sz, void *ud);
/*
** Type for memory-allocation functions
*/
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
typedef void *(*lua_Alloc)(void *ud, void *ptr, size_t osize, size_t nsize);
/*
** Type for warning functions
*/
typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
typedef void (*lua_WarnFunction)(void *ud, const char *msg, int tocont);
/*
@ -140,7 +140,7 @@ typedef struct lua_Debug lua_Debug;
/*
** Functions to be called by the debugger in specific events
*/
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
typedef void (*lua_Hook)(lua_State *L, lua_Debug *ar);
/*
@ -160,53 +160,53 @@ extern const char lua_ident[];
/*
** state manipulation
*/
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
LUA_API void (lua_close) (lua_State *L);
LUA_API lua_State *(lua_newthread) (lua_State *L);
LUA_API int (lua_closethread) (lua_State *L, lua_State *from);
LUA_API int (lua_resetthread) (lua_State *L); /* Deprecated! */
LUA_API lua_State *(lua_newstate)(lua_Alloc f, void *ud);
LUA_API void (lua_close)(lua_State *L);
LUA_API lua_State *(lua_newthread)(lua_State *L);
LUA_API int (lua_closethread)(lua_State *L, lua_State *from);
LUA_API int (lua_resetthread)(lua_State *L); /* Deprecated! */
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
LUA_API lua_CFunction(lua_atpanic)(lua_State *L, lua_CFunction panicf);
LUA_API lua_Number (lua_version) (lua_State *L);
LUA_API lua_Number(lua_version)(lua_State *L);
/*
** basic stack manipulation
*/
LUA_API int (lua_absindex) (lua_State *L, int idx);
LUA_API int (lua_gettop) (lua_State *L);
LUA_API void (lua_settop) (lua_State *L, int idx);
LUA_API void (lua_pushvalue) (lua_State *L, int idx);
LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
LUA_API int (lua_checkstack) (lua_State *L, int n);
LUA_API int (lua_absindex)(lua_State *L, int idx);
LUA_API int (lua_gettop)(lua_State *L);
LUA_API void (lua_settop)(lua_State *L, int idx);
LUA_API void (lua_pushvalue)(lua_State *L, int idx);
LUA_API void (lua_rotate)(lua_State *L, int idx, int n);
LUA_API void (lua_copy)(lua_State *L, int fromidx, int toidx);
LUA_API int (lua_checkstack)(lua_State *L, int n);
LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
LUA_API void (lua_xmove)(lua_State *from, lua_State *to, int n);
/*
** access functions (stack -> C)
*/
LUA_API int (lua_isnumber) (lua_State *L, int idx);
LUA_API int (lua_isstring) (lua_State *L, int idx);
LUA_API int (lua_iscfunction) (lua_State *L, int idx);
LUA_API int (lua_isinteger) (lua_State *L, int idx);
LUA_API int (lua_isuserdata) (lua_State *L, int idx);
LUA_API int (lua_type) (lua_State *L, int idx);
LUA_API const char *(lua_typename) (lua_State *L, int tp);
LUA_API int (lua_isnumber)(lua_State *L, int idx);
LUA_API int (lua_isstring)(lua_State *L, int idx);
LUA_API int (lua_iscfunction)(lua_State *L, int idx);
LUA_API int (lua_isinteger)(lua_State *L, int idx);
LUA_API int (lua_isuserdata)(lua_State *L, int idx);
LUA_API int (lua_type)(lua_State *L, int idx);
LUA_API const char *(lua_typename)(lua_State *L, int tp);
LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
LUA_API int (lua_toboolean) (lua_State *L, int idx);
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);
LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
LUA_API void *(lua_touserdata) (lua_State *L, int idx);
LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
LUA_API const void *(lua_topointer) (lua_State *L, int idx);
LUA_API lua_Number(lua_tonumberx)(lua_State *L, int idx, int *isnum);
LUA_API lua_Integer(lua_tointegerx)(lua_State *L, int idx, int *isnum);
LUA_API int (lua_toboolean)(lua_State *L, int idx);
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);
LUA_API lua_CFunction(lua_tocfunction)(lua_State *L, int idx);
LUA_API void *(lua_touserdata)(lua_State *L, int idx);
LUA_API lua_State *(lua_tothread)(lua_State *L, int idx);
LUA_API const void *(lua_topointer)(lua_State *L, int idx);
/*
@ -228,90 +228,90 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx);
#define LUA_OPUNM 12
#define LUA_OPBNOT 13
LUA_API void (lua_arith) (lua_State *L, int op);
LUA_API void (lua_arith)(lua_State *L, int op);
#define LUA_OPEQ 0
#define LUA_OPLT 1
#define LUA_OPLE 2
LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
LUA_API int (lua_rawequal)(lua_State *L, int idx1, int idx2);
LUA_API int (lua_compare)(lua_State *L, int idx1, int idx2, int op);
/*
** push functions (C -> stack)
*/
LUA_API void (lua_pushnil) (lua_State *L);
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
LUA_API void (lua_pushnil)(lua_State *L);
LUA_API void (lua_pushnumber)(lua_State *L, lua_Number n);
LUA_API void (lua_pushinteger)(lua_State *L, lua_Integer n);
LUA_API const char *(lua_pushlstring)(lua_State *L, const char *s, size_t len);
LUA_API const char *(lua_pushstring)(lua_State *L, const char *s);
LUA_API const char *(lua_pushvfstring)(lua_State *L, const char *fmt,
va_list argp);
LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
LUA_API void (lua_pushboolean) (lua_State *L, int b);
LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
LUA_API int (lua_pushthread) (lua_State *L);
LUA_API const char *(lua_pushfstring)(lua_State *L, const char *fmt, ...);
LUA_API void (lua_pushcclosure)(lua_State *L, lua_CFunction fn, int n);
LUA_API void (lua_pushboolean)(lua_State *L, int b);
LUA_API void (lua_pushlightuserdata)(lua_State *L, void *p);
LUA_API int (lua_pushthread)(lua_State *L);
/*
** get functions (Lua -> stack)
*/
LUA_API int (lua_getglobal) (lua_State *L, const char *name);
LUA_API int (lua_gettable) (lua_State *L, int idx);
LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
LUA_API int (lua_rawget) (lua_State *L, int idx);
LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
LUA_API int (lua_getglobal)(lua_State *L, const char *name);
LUA_API int (lua_gettable)(lua_State *L, int idx);
LUA_API int (lua_getfield)(lua_State *L, int idx, const char *k);
LUA_API int (lua_geti)(lua_State *L, int idx, lua_Integer n);
LUA_API int (lua_rawget)(lua_State *L, int idx);
LUA_API int (lua_rawgeti)(lua_State *L, int idx, lua_Integer n);
LUA_API int (lua_rawgetp)(lua_State *L, int idx, const void *p);
LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n);
LUA_API void (lua_createtable)(lua_State *L, int narr, int nrec);
LUA_API void *(lua_newuserdatauv)(lua_State *L, size_t sz, int nuvalue);
LUA_API int (lua_getmetatable)(lua_State *L, int objindex);
LUA_API int (lua_getiuservalue)(lua_State *L, int idx, int n);
/*
** set functions (stack -> Lua)
*/
LUA_API void (lua_setglobal) (lua_State *L, const char *name);
LUA_API void (lua_settable) (lua_State *L, int idx);
LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
LUA_API void (lua_seti) (lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawset) (lua_State *L, int idx);
LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n);
LUA_API void (lua_setglobal)(lua_State *L, const char *name);
LUA_API void (lua_settable)(lua_State *L, int idx);
LUA_API void (lua_setfield)(lua_State *L, int idx, const char *k);
LUA_API void (lua_seti)(lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawset)(lua_State *L, int idx);
LUA_API void (lua_rawseti)(lua_State *L, int idx, lua_Integer n);
LUA_API void (lua_rawsetp)(lua_State *L, int idx, const void *p);
LUA_API int (lua_setmetatable)(lua_State *L, int objindex);
LUA_API int (lua_setiuservalue)(lua_State *L, int idx, int n);
/*
** 'load' and 'call' functions (load and run Lua code)
*/
LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults,
LUA_API void (lua_callk)(lua_State *L, int nargs, int nresults,
lua_KContext ctx, lua_KFunction k);
#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
LUA_API int (lua_pcallk)(lua_State *L, int nargs, int nresults, int errfunc,
lua_KContext ctx, lua_KFunction k);
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
LUA_API int (lua_load)(lua_State *L, lua_Reader reader, void *dt,
const char *chunkname, const char *mode);
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
LUA_API int (lua_dump)(lua_State *L, lua_Writer writer, void *data, int strip);
/*
** coroutine functions
*/
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
LUA_API int (lua_yieldk)(lua_State *L, int nresults, lua_KContext ctx,
lua_KFunction k);
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg,
LUA_API int (lua_resume)(lua_State *L, lua_State *from, int narg,
int *nres);
LUA_API int (lua_status) (lua_State *L);
LUA_API int (lua_isyieldable) (lua_State *L);
LUA_API int (lua_status)(lua_State *L);
LUA_API int (lua_isyieldable)(lua_State *L);
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
@ -319,8 +319,8 @@ LUA_API int (lua_isyieldable) (lua_State *L);
/*
** Warning-related functions
*/
LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud);
LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
LUA_API void (lua_setwarnf)(lua_State *L, lua_WarnFunction f, void *ud);
LUA_API void (lua_warning)(lua_State *L, const char *msg, int tocont);
/*
@ -339,27 +339,27 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
#define LUA_GCGEN 10
#define LUA_GCINC 11
LUA_API int (lua_gc) (lua_State *L, int what, ...);
LUA_API int (lua_gc)(lua_State *L, int what, ...);
/*
** miscellaneous functions
*/
LUA_API int (lua_error) (lua_State *L);
LUA_API int (lua_error)(lua_State *L);
LUA_API int (lua_next) (lua_State *L, int idx);
LUA_API int (lua_next)(lua_State *L, int idx);
LUA_API void (lua_concat) (lua_State *L, int n);
LUA_API void (lua_len) (lua_State *L, int idx);
LUA_API void (lua_concat)(lua_State *L, int n);
LUA_API void (lua_len)(lua_State *L, int idx);
LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
LUA_API size_t (lua_stringtonumber)(lua_State *L, const char *s);
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
LUA_API lua_Alloc(lua_getallocf)(lua_State *L, void **ud);
LUA_API void (lua_setallocf)(lua_State *L, lua_Alloc f, void *ud);
LUA_API void (lua_toclose) (lua_State *L, int idx);
LUA_API void (lua_closeslot) (lua_State *L, int idx);
LUA_API void (lua_toclose)(lua_State *L, int idx);
LUA_API void (lua_closeslot)(lua_State *L, int idx);
/*
@ -454,23 +454,23 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx);
#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n);
LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
LUA_API int (lua_getstack)(lua_State *L, int level, lua_Debug *ar);
LUA_API int (lua_getinfo)(lua_State *L, const char *what, lua_Debug *ar);
LUA_API const char *(lua_getlocal)(lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *(lua_setlocal)(lua_State *L, const lua_Debug *ar, int n);
LUA_API const char *(lua_getupvalue)(lua_State *L, int funcindex, int n);
LUA_API const char *(lua_setupvalue)(lua_State *L, int funcindex, int n);
LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n);
LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1,
LUA_API void *(lua_upvalueid)(lua_State *L, int fidx, int n);
LUA_API void (lua_upvaluejoin)(lua_State *L, int fidx1, int n1,
int fidx2, int n2);
LUA_API void (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);
LUA_API lua_Hook (lua_gethook) (lua_State *L);
LUA_API int (lua_gethookmask) (lua_State *L);
LUA_API int (lua_gethookcount) (lua_State *L);
LUA_API void (lua_sethook)(lua_State *L, lua_Hook func, int mask, int count);
LUA_API lua_Hook(lua_gethook)(lua_State *L);
LUA_API int (lua_gethookmask)(lua_State *L);
LUA_API int (lua_gethookcount)(lua_State *L);
LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit);
LUA_API int (lua_setcstacklimit)(lua_State *L, unsigned int limit);
struct lua_Debug {
int event;

View file

@ -25,38 +25,35 @@
#include "lstate.h"
#include "lundump.h"
static void PrintFunction(const Proto* f, int full);
static void PrintFunction(const Proto *f, int full);
#define luaU_print PrintFunction
#define PROGNAME "luac" /* default program name */
#define OUTPUT PROGNAME ".out" /* default output file */
static int listing=0; /* list bytecodes? */
static int dumping=1; /* dump bytecodes? */
static int stripping=0; /* strip debug information? */
static char Output[]={ OUTPUT }; /* default output file name */
static const char* output=Output; /* actual output file name */
static const char* progname=PROGNAME; /* actual program name */
static int listing = 0; /* list bytecodes? */
static int dumping = 1; /* dump bytecodes? */
static int stripping = 0; /* strip debug information? */
static char Output[] = { OUTPUT }; /* default output file name */
static const char *output = Output; /* actual output file name */
static const char *progname = PROGNAME; /* actual program name */
static TString **tmname;
static void fatal(const char* message)
{
fprintf(stderr,"%s: %s\n",progname,message);
static void fatal(const char *message) {
fprintf(stderr, "%s: %s\n", progname, message);
exit(EXIT_FAILURE);
}
static void cannot(const char* what)
{
fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
static void cannot(const char *what) {
fprintf(stderr, "%s: cannot %s %s: %s\n", progname, what, output, strerror(errno));
exit(EXIT_FAILURE);
}
static void usage(const char* message)
{
if (*message=='-')
fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message);
static void usage(const char *message) {
if (*message == '-')
fprintf(stderr, "%s: unrecognized option '%s'\n", progname, message);
else
fprintf(stderr,"%s: %s\n",progname,message);
fprintf(stderr, "%s: %s\n", progname, message);
fprintf(stderr,
"usage: %s [options] [filenames]\n"
"Available options are:\n"
@ -67,125 +64,106 @@ static void usage(const char* message)
" -v show version information\n"
" -- stop handling options\n"
" - stop handling options and process stdin\n"
,progname,Output);
, progname, Output);
exit(EXIT_FAILURE);
}
#define IS(s) (strcmp(argv[i],s)==0)
static int doargs(int argc, char* argv[])
{
static int doargs(int argc, char *argv[]) {
int i;
int version=0;
if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
for (i=1; i<argc; i++)
{
if (*argv[i]!='-') /* end of options; keep it */
int version = 0;
if (argv[0] != NULL && *argv[0] != 0) progname = argv[0];
for (i = 1; i < argc; i++) {
if (*argv[i] != '-') /* end of options; keep it */
break;
else if (IS("--")) /* end of options; skip it */
{
else if (IS("--")) { /* end of options; skip it */
++i;
if (version) ++version;
break;
}
else if (IS("-")) /* end of options; use stdin */
} else if (IS("-")) /* end of options; use stdin */
break;
else if (IS("-l")) /* list */
++listing;
else if (IS("-o")) /* output file */
{
output=argv[++i];
if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
else if (IS("-o")) { /* output file */
output = argv[++i];
if (output == NULL || *output == 0 || (*output == '-' && output[1] != 0))
usage("'-o' needs argument");
if (IS("-")) output=NULL;
}
else if (IS("-p")) /* parse only */
dumping=0;
if (IS("-")) output = NULL;
} else if (IS("-p")) /* parse only */
dumping = 0;
else if (IS("-s")) /* strip debug information */
stripping=1;
stripping = 1;
else if (IS("-v")) /* show version */
++version;
else /* unknown option */
usage(argv[i]);
}
if (i==argc && (listing || !dumping))
{
dumping=0;
argv[--i]=Output;
if (i == argc && (listing || !dumping)) {
dumping = 0;
argv[--i] = Output;
}
if (version)
{
printf("%s\n",LUA_COPYRIGHT);
if (version==argc-1) exit(EXIT_SUCCESS);
if (version) {
printf("%s\n", LUA_COPYRIGHT);
if (version == argc - 1) exit(EXIT_SUCCESS);
}
return i;
}
#define FUNCTION "(function()end)();\n"
static const char* reader(lua_State* L, void* ud, size_t* size)
{
static const char *reader(lua_State *L, void *ud, size_t *size) {
UNUSED(L);
if ((*(int*)ud)--)
{
*size=sizeof(FUNCTION)-1;
if ((*(int *)ud)--) {
*size = sizeof(FUNCTION) - 1;
return FUNCTION;
}
else
{
*size=0;
} else {
*size = 0;
return NULL;
}
}
#define toproto(L,i) getproto(s2v(L->top.p+(i)))
static const Proto* combine(lua_State* L, int n)
{
if (n==1)
return toproto(L,-1);
else
{
Proto* f;
int i=n;
if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
f=toproto(L,-1);
for (i=0; i<n; i++)
{
f->p[i]=toproto(L,i-n-1);
if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
static const Proto *combine(lua_State *L, int n) {
if (n == 1)
return toproto(L, -1);
else {
Proto *f;
int i = n;
if (lua_load(L, reader, &i, "=(" PROGNAME ")", NULL) != LUA_OK) fatal(lua_tostring(L, -1));
f = toproto(L, -1);
for (i = 0; i < n; i++) {
f->p[i] = toproto(L, i - n - 1);
if (f->p[i]->sizeupvalues > 0) f->p[i]->upvalues[0].instack = 0;
}
return f;
}
}
static int writer(lua_State* L, const void* p, size_t size, void* u)
{
static int writer(lua_State *L, const void *p, size_t size, void *u) {
UNUSED(L);
return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
return (fwrite(p, size, 1, (FILE *)u) != 1) && (size != 0);
}
static int pmain(lua_State* L)
{
int argc=(int)lua_tointeger(L,1);
char** argv=(char**)lua_touserdata(L,2);
const Proto* f;
static int pmain(lua_State *L) {
int argc = (int)lua_tointeger(L, 1);
char **argv = (char **)lua_touserdata(L, 2);
const Proto *f;
int i;
tmname=G(L)->tmname;
if (!lua_checkstack(L,argc)) fatal("too many input files");
for (i=0; i<argc; i++)
{
const char* filename=IS("-") ? NULL : argv[i];
if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
tmname = G(L)->tmname;
if (!lua_checkstack(L, argc)) fatal("too many input files");
for (i = 0; i < argc; i++) {
const char *filename = IS("-") ? NULL : argv[i];
if (luaL_loadfile(L, filename) != LUA_OK) fatal(lua_tostring(L, -1));
}
f=combine(L,argc);
if (listing) luaU_print(f,listing>1);
if (dumping)
{
FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
if (D==NULL) cannot("open");
f = combine(L, argc);
if (listing) luaU_print(f, listing > 1);
if (dumping) {
FILE *D = (output == NULL) ? stdout : fopen(output, "wb");
if (D == NULL) cannot("open");
lua_lock(L);
luaU_dump(L,f,writer,D,stripping);
luaU_dump(L, f, writer, D, stripping);
lua_unlock(L);
if (ferror(D)) cannot("write");
if (fclose(D)) cannot("close");
@ -193,18 +171,18 @@ static int pmain(lua_State* L)
return 0;
}
int main(int argc, char* argv[])
{
lua_State* L;
int i=doargs(argc,argv);
argc-=i; argv+=i;
if (argc<=0) usage("no input files given");
L=luaL_newstate();
if (L==NULL) fatal("cannot create state: not enough memory");
lua_pushcfunction(L,&pmain);
lua_pushinteger(L,argc);
lua_pushlightuserdata(L,argv);
if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
int main(int argc, char *argv[]) {
lua_State *L;
int i = doargs(argc, argv);
argc -= i;
argv += i;
if (argc <= 0) usage("no input files given");
L = luaL_newstate();
if (L == NULL) fatal("cannot create state: not enough memory");
lua_pushcfunction(L, &pmain);
lua_pushinteger(L, argc);
lua_pushlightuserdata(L, argv);
if (lua_pcall(L, 2, 0, 0) != LUA_OK) fatal(lua_tostring(L, -1));
lua_close(L);
return EXIT_SUCCESS;
}
@ -217,16 +195,13 @@ int main(int argc, char* argv[])
#define VOID(p) ((const void*)(p))
#define eventname(i) (getstr(tmname[i]))
static void PrintString(const TString* ts)
{
const char* s=getstr(ts);
size_t i,n=tsslen(ts);
static void PrintString(const TString *ts) {
const char *s = getstr(ts);
size_t i, n = tsslen(ts);
printf("\"");
for (i=0; i<n; i++)
{
int c=(int)(unsigned char)s[i];
switch (c)
{
for (i = 0; i < n; i++) {
int c = (int)(unsigned char)s[i];
switch (c) {
case '"':
printf("\\\"");
break;
@ -255,18 +230,17 @@ static void PrintString(const TString* ts)
printf("\\v");
break;
default:
if (isprint(c)) printf("%c",c); else printf("\\%03d",c);
if (isprint(c)) printf("%c", c);
else printf("\\%03d", c);
break;
}
}
printf("\"");
}
static void PrintType(const Proto* f, int i)
{
const TValue* o=&f->k[i];
switch (ttypetag(o))
{
static void PrintType(const Proto *f, int i) {
const TValue *o = &f->k[i];
switch (ttypetag(o)) {
case LUA_VNIL:
printf("N");
break;
@ -285,17 +259,15 @@ static void PrintType(const Proto* f, int i)
printf("S");
break;
default: /* cannot happen */
printf("?%d",ttypetag(o));
printf("?%d", ttypetag(o));
break;
}
printf("\t");
}
static void PrintConstant(const Proto* f, int i)
{
const TValue* o=&f->k[i];
switch (ttypetag(o))
{
static void PrintConstant(const Proto *f, int i) {
const TValue *o = &f->k[i];
switch (ttypetag(o)) {
case LUA_VNIL:
printf("nil");
break;
@ -305,23 +277,22 @@ static void PrintConstant(const Proto* f, int i)
case LUA_VTRUE:
printf("true");
break;
case LUA_VNUMFLT:
{
case LUA_VNUMFLT: {
char buff[100];
sprintf(buff,LUA_NUMBER_FMT,fltvalue(o));
printf("%s",buff);
if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
sprintf(buff, LUA_NUMBER_FMT, fltvalue(o));
printf("%s", buff);
if (buff[strspn(buff, "-0123456789")] == '\0') printf(".0");
break;
}
case LUA_VNUMINT:
printf(LUA_INTEGER_FMT,ivalue(o));
printf(LUA_INTEGER_FMT, ivalue(o));
break;
case LUA_VSHRSTR:
case LUA_VLNGSTR:
PrintString(tsvalue(o));
break;
default: /* cannot happen */
printf("?%d",ttypetag(o));
printf("?%d", ttypetag(o));
break;
}
}
@ -331,328 +302,348 @@ static void PrintConstant(const Proto* f, int i)
#define EXTRAARGC (EXTRAARG*(MAXARG_C+1))
#define ISK (isk ? "k" : "")
static void PrintCode(const Proto* f)
{
const Instruction* code=f->code;
int pc,n=f->sizecode;
for (pc=0; pc<n; pc++)
{
Instruction i=code[pc];
OpCode o=GET_OPCODE(i);
int a=GETARG_A(i);
int b=GETARG_B(i);
int c=GETARG_C(i);
int ax=GETARG_Ax(i);
int bx=GETARG_Bx(i);
int sb=GETARG_sB(i);
int sc=GETARG_sC(i);
int sbx=GETARG_sBx(i);
int isk=GETARG_k(i);
int line=luaG_getfuncline(f,pc);
printf("\t%d\t",pc+1);
if (line>0) printf("[%d]\t",line); else printf("[-]\t");
printf("%-9s\t",opnames[o]);
switch (o)
{
static void PrintCode(const Proto *f) {
const Instruction *code = f->code;
int pc, n = f->sizecode;
for (pc = 0; pc < n; pc++) {
Instruction i = code[pc];
OpCode o = GET_OPCODE(i);
int a = GETARG_A(i);
int b = GETARG_B(i);
int c = GETARG_C(i);
int ax = GETARG_Ax(i);
int bx = GETARG_Bx(i);
int sb = GETARG_sB(i);
int sc = GETARG_sC(i);
int sbx = GETARG_sBx(i);
int isk = GETARG_k(i);
int line = luaG_getfuncline(f, pc);
printf("\t%d\t", pc + 1);
if (line > 0) printf("[%d]\t", line);
else printf("[-]\t");
printf("%-9s\t", opnames[o]);
switch (o) {
case OP_MOVE:
printf("%d %d",a,b);
printf("%d %d", a, b);
break;
case OP_LOADI:
printf("%d %d",a,sbx);
printf("%d %d", a, sbx);
break;
case OP_LOADF:
printf("%d %d",a,sbx);
printf("%d %d", a, sbx);
break;
case OP_LOADK:
printf("%d %d",a,bx);
printf(COMMENT); PrintConstant(f,bx);
printf("%d %d", a, bx);
printf(COMMENT);
PrintConstant(f, bx);
break;
case OP_LOADKX:
printf("%d",a);
printf(COMMENT); PrintConstant(f,EXTRAARG);
printf("%d", a);
printf(COMMENT);
PrintConstant(f, EXTRAARG);
break;
case OP_LOADFALSE:
printf("%d",a);
printf("%d", a);
break;
case OP_LFALSESKIP:
printf("%d",a);
printf("%d", a);
break;
case OP_LOADTRUE:
printf("%d",a);
printf("%d", a);
break;
case OP_LOADNIL:
printf("%d %d",a,b);
printf(COMMENT "%d out",b+1);
printf("%d %d", a, b);
printf(COMMENT "%d out", b + 1);
break;
case OP_GETUPVAL:
printf("%d %d",a,b);
printf(COMMENT "%s",UPVALNAME(b));
printf("%d %d", a, b);
printf(COMMENT "%s", UPVALNAME(b));
break;
case OP_SETUPVAL:
printf("%d %d",a,b);
printf(COMMENT "%s",UPVALNAME(b));
printf("%d %d", a, b);
printf(COMMENT "%s", UPVALNAME(b));
break;
case OP_GETTABUP:
printf("%d %d %d",a,b,c);
printf(COMMENT "%s",UPVALNAME(b));
printf(" "); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT "%s", UPVALNAME(b));
printf(" ");
PrintConstant(f, c);
break;
case OP_GETTABLE:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_GETI:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_GETFIELD:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_SETTABUP:
printf("%d %d %d%s",a,b,c,ISK);
printf(COMMENT "%s",UPVALNAME(a));
printf(" "); PrintConstant(f,b);
if (isk) { printf(" "); PrintConstant(f,c); }
printf("%d %d %d%s", a, b, c, ISK);
printf(COMMENT "%s", UPVALNAME(a));
printf(" ");
PrintConstant(f, b);
if (isk) { printf(" "); PrintConstant(f, c); }
break;
case OP_SETTABLE:
printf("%d %d %d%s",a,b,c,ISK);
if (isk) { printf(COMMENT); PrintConstant(f,c); }
printf("%d %d %d%s", a, b, c, ISK);
if (isk) { printf(COMMENT); PrintConstant(f, c); }
break;
case OP_SETI:
printf("%d %d %d%s",a,b,c,ISK);
if (isk) { printf(COMMENT); PrintConstant(f,c); }
printf("%d %d %d%s", a, b, c, ISK);
if (isk) { printf(COMMENT); PrintConstant(f, c); }
break;
case OP_SETFIELD:
printf("%d %d %d%s",a,b,c,ISK);
printf(COMMENT); PrintConstant(f,b);
if (isk) { printf(" "); PrintConstant(f,c); }
printf("%d %d %d%s", a, b, c, ISK);
printf(COMMENT);
PrintConstant(f, b);
if (isk) { printf(" "); PrintConstant(f, c); }
break;
case OP_NEWTABLE:
printf("%d %d %d",a,b,c);
printf(COMMENT "%d",c+EXTRAARGC);
printf("%d %d %d", a, b, c);
printf(COMMENT "%d", c + EXTRAARGC);
break;
case OP_SELF:
printf("%d %d %d%s",a,b,c,ISK);
if (isk) { printf(COMMENT); PrintConstant(f,c); }
printf("%d %d %d%s", a, b, c, ISK);
if (isk) { printf(COMMENT); PrintConstant(f, c); }
break;
case OP_ADDI:
printf("%d %d %d",a,b,sc);
printf("%d %d %d", a, b, sc);
break;
case OP_ADDK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_SUBK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_MULK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_MODK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_POWK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_DIVK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_IDIVK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_BANDK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_BORK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_BXORK:
printf("%d %d %d",a,b,c);
printf(COMMENT); PrintConstant(f,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
PrintConstant(f, c);
break;
case OP_SHRI:
printf("%d %d %d",a,b,sc);
printf("%d %d %d", a, b, sc);
break;
case OP_SHLI:
printf("%d %d %d",a,b,sc);
printf("%d %d %d", a, b, sc);
break;
case OP_ADD:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_SUB:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_MUL:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_MOD:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_POW:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_DIV:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_IDIV:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_BAND:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_BOR:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_BXOR:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_SHL:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_SHR:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
break;
case OP_MMBIN:
printf("%d %d %d",a,b,c);
printf(COMMENT "%s",eventname(c));
printf("%d %d %d", a, b, c);
printf(COMMENT "%s", eventname(c));
break;
case OP_MMBINI:
printf("%d %d %d %d",a,sb,c,isk);
printf(COMMENT "%s",eventname(c));
printf("%d %d %d %d", a, sb, c, isk);
printf(COMMENT "%s", eventname(c));
if (isk) printf(" flip");
break;
case OP_MMBINK:
printf("%d %d %d %d",a,b,c,isk);
printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b);
printf("%d %d %d %d", a, b, c, isk);
printf(COMMENT "%s ", eventname(c));
PrintConstant(f, b);
if (isk) printf(" flip");
break;
case OP_UNM:
printf("%d %d",a,b);
printf("%d %d", a, b);
break;
case OP_BNOT:
printf("%d %d",a,b);
printf("%d %d", a, b);
break;
case OP_NOT:
printf("%d %d",a,b);
printf("%d %d", a, b);
break;
case OP_LEN:
printf("%d %d",a,b);
printf("%d %d", a, b);
break;
case OP_CONCAT:
printf("%d %d",a,b);
printf("%d %d", a, b);
break;
case OP_CLOSE:
printf("%d",a);
printf("%d", a);
break;
case OP_TBC:
printf("%d",a);
printf("%d", a);
break;
case OP_JMP:
printf("%d",GETARG_sJ(i));
printf(COMMENT "to %d",GETARG_sJ(i)+pc+2);
printf("%d", GETARG_sJ(i));
printf(COMMENT "to %d", GETARG_sJ(i) + pc + 2);
break;
case OP_EQ:
printf("%d %d %d",a,b,isk);
printf("%d %d %d", a, b, isk);
break;
case OP_LT:
printf("%d %d %d",a,b,isk);
printf("%d %d %d", a, b, isk);
break;
case OP_LE:
printf("%d %d %d",a,b,isk);
printf("%d %d %d", a, b, isk);
break;
case OP_EQK:
printf("%d %d %d",a,b,isk);
printf(COMMENT); PrintConstant(f,b);
printf("%d %d %d", a, b, isk);
printf(COMMENT);
PrintConstant(f, b);
break;
case OP_EQI:
printf("%d %d %d",a,sb,isk);
printf("%d %d %d", a, sb, isk);
break;
case OP_LTI:
printf("%d %d %d",a,sb,isk);
printf("%d %d %d", a, sb, isk);
break;
case OP_LEI:
printf("%d %d %d",a,sb,isk);
printf("%d %d %d", a, sb, isk);
break;
case OP_GTI:
printf("%d %d %d",a,sb,isk);
printf("%d %d %d", a, sb, isk);
break;
case OP_GEI:
printf("%d %d %d",a,sb,isk);
printf("%d %d %d", a, sb, isk);
break;
case OP_TEST:
printf("%d %d",a,isk);
printf("%d %d", a, isk);
break;
case OP_TESTSET:
printf("%d %d %d",a,b,isk);
printf("%d %d %d", a, b, isk);
break;
case OP_CALL:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
printf(COMMENT);
if (b==0) printf("all in "); else printf("%d in ",b-1);
if (c==0) printf("all out"); else printf("%d out",c-1);
if (b == 0) printf("all in ");
else printf("%d in ", b - 1);
if (c == 0) printf("all out");
else printf("%d out", c - 1);
break;
case OP_TAILCALL:
printf("%d %d %d%s",a,b,c,ISK);
printf(COMMENT "%d in",b-1);
printf("%d %d %d%s", a, b, c, ISK);
printf(COMMENT "%d in", b - 1);
break;
case OP_RETURN:
printf("%d %d %d%s",a,b,c,ISK);
printf("%d %d %d%s", a, b, c, ISK);
printf(COMMENT);
if (b==0) printf("all out"); else printf("%d out",b-1);
if (b == 0) printf("all out");
else printf("%d out", b - 1);
break;
case OP_RETURN0:
break;
case OP_RETURN1:
printf("%d",a);
printf("%d", a);
break;
case OP_FORLOOP:
printf("%d %d",a,bx);
printf(COMMENT "to %d",pc-bx+2);
printf("%d %d", a, bx);
printf(COMMENT "to %d", pc - bx + 2);
break;
case OP_FORPREP:
printf("%d %d",a,bx);
printf(COMMENT "exit to %d",pc+bx+3);
printf("%d %d", a, bx);
printf(COMMENT "exit to %d", pc + bx + 3);
break;
case OP_TFORPREP:
printf("%d %d",a,bx);
printf(COMMENT "to %d",pc+bx+2);
printf("%d %d", a, bx);
printf(COMMENT "to %d", pc + bx + 2);
break;
case OP_TFORCALL:
printf("%d %d",a,c);
printf("%d %d", a, c);
break;
case OP_TFORLOOP:
printf("%d %d",a,bx);
printf(COMMENT "to %d",pc-bx+2);
printf("%d %d", a, bx);
printf(COMMENT "to %d", pc - bx + 2);
break;
case OP_SETLIST:
printf("%d %d %d",a,b,c);
if (isk) printf(COMMENT "%d",c+EXTRAARGC);
printf("%d %d %d", a, b, c);
if (isk) printf(COMMENT "%d", c + EXTRAARGC);
break;
case OP_CLOSURE:
printf("%d %d",a,bx);
printf(COMMENT "%p",VOID(f->p[bx]));
printf("%d %d", a, bx);
printf(COMMENT "%p", VOID(f->p[bx]));
break;
case OP_VARARG:
printf("%d %d",a,c);
printf("%d %d", a, c);
printf(COMMENT);
if (c==0) printf("all out"); else printf("%d out",c-1);
if (c == 0) printf("all out");
else printf("%d out", c - 1);
break;
case OP_VARARGPREP:
printf("%d",a);
printf("%d", a);
break;
case OP_EXTRAARG:
printf("%d",ax);
printf("%d", ax);
break;
#if 0
default:
printf("%d %d %d",a,b,c);
printf("%d %d %d", a, b, c);
printf(COMMENT "not handled");
break;
#endif
@ -665,59 +656,53 @@ static void PrintCode(const Proto* f)
#define SS(x) ((x==1)?"":"s")
#define S(x) (int)(x),SS(x)
static void PrintHeader(const Proto* f)
{
const char* s=f->source ? getstr(f->source) : "=?";
if (*s=='@' || *s=='=')
static void PrintHeader(const Proto *f) {
const char *s = f->source ? getstr(f->source) : "=?";
if (*s == '@' || *s == '=')
s++;
else if (*s==LUA_SIGNATURE[0])
s="(bstring)";
else if (*s == LUA_SIGNATURE[0])
s = "(bstring)";
else
s="(string)";
s = "(string)";
printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
(f->linedefined==0)?"main":"function",s,
f->linedefined,f->lastlinedefined,
S(f->sizecode),VOID(f));
(f->linedefined == 0) ? "main" : "function", s,
f->linedefined, f->lastlinedefined,
S(f->sizecode), VOID(f));
printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
(int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
S(f->maxstacksize),S(f->sizeupvalues));
(int)(f->numparams), f->is_vararg ? "+" : "", SS(f->numparams),
S(f->maxstacksize), S(f->sizeupvalues));
printf("%d local%s, %d constant%s, %d function%s\n",
S(f->sizelocvars),S(f->sizek),S(f->sizep));
S(f->sizelocvars), S(f->sizek), S(f->sizep));
}
static void PrintDebug(const Proto* f)
{
int i,n;
n=f->sizek;
printf("constants (%d) for %p:\n",n,VOID(f));
for (i=0; i<n; i++)
{
printf("\t%d\t",i);
PrintType(f,i);
PrintConstant(f,i);
static void PrintDebug(const Proto *f) {
int i, n;
n = f->sizek;
printf("constants (%d) for %p:\n", n, VOID(f));
for (i = 0; i < n; i++) {
printf("\t%d\t", i);
PrintType(f, i);
PrintConstant(f, i);
printf("\n");
}
n=f->sizelocvars;
printf("locals (%d) for %p:\n",n,VOID(f));
for (i=0; i<n; i++)
{
n = f->sizelocvars;
printf("locals (%d) for %p:\n", n, VOID(f));
for (i = 0; i < n; i++) {
printf("\t%d\t%s\t%d\t%d\n",
i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
i, getstr(f->locvars[i].varname), f->locvars[i].startpc + 1, f->locvars[i].endpc + 1);
}
n=f->sizeupvalues;
printf("upvalues (%d) for %p:\n",n,VOID(f));
for (i=0; i<n; i++)
{
n = f->sizeupvalues;
printf("upvalues (%d) for %p:\n", n, VOID(f));
for (i = 0; i < n; i++) {
printf("\t%d\t%s\t%d\t%d\n",
i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
i, UPVALNAME(i), f->upvalues[i].instack, f->upvalues[i].idx);
}
}
static void PrintFunction(const Proto* f, int full)
{
int i,n=f->sizep;
static void PrintFunction(const Proto *f, int full) {
int i, n = f->sizep;
PrintHeader(f);
PrintCode(f);
if (full) PrintDebug(f);
for (i=0; i<n; i++) PrintFunction(f->p[i],full);
for (i = 0; i < n; i++) PrintFunction(f->p[i], full);
}

View file

@ -570,7 +570,7 @@
#else /* }{ */
#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)"
or '-DLUA_C89_NUMBERS'(see file 'luaconf.h' for details)"
#endif /* } */

View file

@ -15,38 +15,38 @@
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
LUAMOD_API int (luaopen_base) (lua_State *L);
LUAMOD_API int (luaopen_base)(lua_State *L);
#define LUA_COLIBNAME "coroutine"
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
LUAMOD_API int (luaopen_coroutine)(lua_State *L);
#define LUA_TABLIBNAME "table"
LUAMOD_API int (luaopen_table) (lua_State *L);
LUAMOD_API int (luaopen_table)(lua_State *L);
#define LUA_IOLIBNAME "io"
LUAMOD_API int (luaopen_io) (lua_State *L);
LUAMOD_API int (luaopen_io)(lua_State *L);
#define LUA_OSLIBNAME "os"
LUAMOD_API int (luaopen_os) (lua_State *L);
LUAMOD_API int (luaopen_os)(lua_State *L);
#define LUA_STRLIBNAME "string"
LUAMOD_API int (luaopen_string) (lua_State *L);
LUAMOD_API int (luaopen_string)(lua_State *L);
#define LUA_UTF8LIBNAME "utf8"
LUAMOD_API int (luaopen_utf8) (lua_State *L);
LUAMOD_API int (luaopen_utf8)(lua_State *L);
#define LUA_MATHLIBNAME "math"
LUAMOD_API int (luaopen_math) (lua_State *L);
LUAMOD_API int (luaopen_math)(lua_State *L);
#define LUA_DBLIBNAME "debug"
LUAMOD_API int (luaopen_debug) (lua_State *L);
LUAMOD_API int (luaopen_debug)(lua_State *L);
#define LUA_LOADLIBNAME "package"
LUAMOD_API int (luaopen_package) (lua_State *L);
LUAMOD_API int (luaopen_package)(lua_State *L);
/* open all previous libraries */
LUALIB_API void (luaL_openlibs) (lua_State *L);
LUALIB_API void (luaL_openlibs)(lua_State *L);
#endif

View file

@ -37,7 +37,7 @@ typedef struct {
} LoadState;
static l_noret error (LoadState *S, const char *why) {
static l_noret error(LoadState *S, const char *why) {
luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
luaD_throw(S->L, LUA_ERRSYNTAX);
}
@ -49,7 +49,7 @@ static l_noret error (LoadState *S, const char *why) {
*/
#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");
}
@ -58,7 +58,7 @@ static void loadBlock (LoadState *S, void *b, size_t size) {
#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,7 +66,7 @@ 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;
@ -80,24 +80,24 @@ static size_t loadUnsigned (LoadState *S, size_t limit) {
}
static size_t loadSize (LoadState *S) {
static size_t loadSize(LoadState *S) {
return loadUnsigned(S, MAX_SIZET);
}
static int loadInt (LoadState *S) {
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);
return x;
}
static lua_Integer loadInteger (LoadState *S) {
static lua_Integer loadInteger(LoadState *S) {
lua_Integer x;
loadVar(S, x);
return x;
@ -107,7 +107,7 @@ static lua_Integer loadInteger (LoadState *S) {
/*
** Load a nullable string into prototype 'p'.
*/
static TString *loadStringN (LoadState *S, Proto *p) {
static TString *loadStringN(LoadState *S, Proto *p) {
lua_State *L = S->L;
TString *ts;
size_t size = loadSize(S);
@ -117,8 +117,7 @@ static TString *loadStringN (LoadState *S, Proto *p) {
char buff[LUAI_MAXSHORTLEN];
loadVector(S, buff, size); /* load string into buffer */
ts = luaS_newlstr(L, buff, size); /* create string */
}
else { /* long string */
} else { /* long string */
ts = luaS_createlngstrobj(L, size); /* create string */
setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */
luaD_inctop(L);
@ -133,7 +132,7 @@ static TString *loadStringN (LoadState *S, Proto *p) {
/*
** Load a non-nullable string into prototype 'p'.
*/
static TString *loadString (LoadState *S, Proto *p) {
static TString *loadString(LoadState *S, Proto *p) {
TString *st = loadStringN(S, p);
if (st == NULL)
error(S, "bad format for constant string");
@ -141,7 +140,7 @@ static TString *loadString (LoadState *S, Proto *p) {
}
static void loadCode (LoadState *S, Proto *f) {
static void loadCode(LoadState *S, Proto *f) {
int n = loadInt(S);
f->code = luaM_newvectorchecked(S->L, n, Instruction);
f->sizecode = n;
@ -152,7 +151,7 @@ static void loadCode (LoadState *S, Proto *f) {
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);
f->k = luaM_newvectorchecked(S->L, n, TValue);
@ -182,13 +181,14 @@ static void loadConstants (LoadState *S, Proto *f) {
case LUA_VLNGSTR:
setsvalue2n(S->L, o, loadString(S, f));
break;
default: lua_assert(0);
default:
lua_assert(0);
}
}
}
static void loadProtos (LoadState *S, Proto *f) {
static void loadProtos(LoadState *S, Proto *f) {
int i;
int n = loadInt(S);
f->p = luaM_newvectorchecked(S->L, n, Proto *);
@ -209,7 +209,7 @@ static void loadProtos (LoadState *S, Proto *f) {
** the creation of the error message can call an emergency collection;
** in that case all prototypes must be consistent for the GC.
*/
static void loadUpvalues (LoadState *S, Proto *f) {
static void loadUpvalues(LoadState *S, Proto *f) {
int i, n;
n = loadInt(S);
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
@ -224,7 +224,7 @@ static void loadUpvalues (LoadState *S, Proto *f) {
}
static void loadDebug (LoadState *S, Proto *f) {
static void loadDebug(LoadState *S, Proto *f) {
int i, n;
n = loadInt(S);
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
@ -255,7 +255,7 @@ static void loadDebug (LoadState *S, Proto *f) {
}
static void loadFunction (LoadState *S, Proto *f, TString *psource) {
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 */
@ -272,7 +272,7 @@ static void loadFunction (LoadState *S, Proto *f, TString *psource) {
}
static void checkliteral (LoadState *S, const char *s, const char *msg) {
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);
@ -281,7 +281,7 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) {
}
static void fchecksize (LoadState *S, size_t size, const char *tname) {
static void fchecksize(LoadState *S, size_t size, const char *tname) {
if (loadByte(S) != size)
error(S, luaO_pushfstring(S->L, "%s size mismatch", tname));
}
@ -289,7 +289,7 @@ static void fchecksize (LoadState *S, size_t size, const char *tname) {
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
static void checkHeader (LoadState *S) {
static void checkHeader(LoadState *S) {
/* skip 1st char (already read and checked) */
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
if (loadByte(S) != LUAC_VERSION)

View file

@ -26,10 +26,10 @@
#define LUAC_FORMAT 0 /* this is the official format */
/* load one chunk; from lundump.c */
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
LUAI_FUNC LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name);
/* dump one chunk; from ldump.c */
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
void* data, int strip);
LUAI_FUNC int luaU_dump(lua_State *L, const Proto *f, lua_Writer w,
void *data, int strip);
#endif

View file

@ -44,7 +44,7 @@ typedef unsigned long utfint;
/* from strlib */
/* translate a relative string position: negative means back from end */
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
static lua_Integer u_posrelat(lua_Integer pos, size_t len) {
if (pos >= 0) return pos;
else if (0u - (size_t)pos > len) return 0;
else return (lua_Integer)len + pos + 1;
@ -58,7 +58,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
** entry forces an error for non-ascii bytes with no continuation
** bytes (count == 0).
*/
static const char *utf8_decode (const char *s, utfint *val, int strict) {
static const char *utf8_decode(const char *s, utfint *val, int strict) {
static const utfint limits[] =
{~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
unsigned int c = (unsigned char)s[0];
@ -93,7 +93,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) {
** start in the range [i,j], or nil + current position if 's' is not
** well formed in that interval
*/
static int utflen (lua_State *L) {
static int utflen(lua_State *L) {
lua_Integer n = 0; /* counter for the number of characters */
size_t len; /* string length in bytes */
const char *s = luaL_checklstring(L, 1, &len);
@ -123,7 +123,7 @@ static int utflen (lua_State *L) {
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
** characters that start in the range [i,j]
*/
static int codepoint (lua_State *L) {
static int codepoint(lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
@ -152,7 +152,7 @@ static int codepoint (lua_State *L) {
}
static void pushutfchar (lua_State *L, int arg) {
static void pushutfchar(lua_State *L, int arg) {
lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
lua_pushfstring(L, "%U", (long)code);
@ -162,7 +162,7 @@ static void pushutfchar (lua_State *L, int arg) {
/*
** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
*/
static int utfchar (lua_State *L) {
static int utfchar(lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
if (n == 1) /* optimize common case of single char */
pushutfchar(L, 1);
@ -184,7 +184,7 @@ static int utfchar (lua_State *L) {
** offset(s, n, [i]) -> index where n-th character counting from
** position 'i' starts; 0 means character at 'i'.
*/
static int byteoffset (lua_State *L) {
static int byteoffset(lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Integer n = luaL_checkinteger(L, 2);
@ -195,8 +195,7 @@ static int byteoffset (lua_State *L) {
if (n == 0) {
/* find beginning of current byte sequence */
while (posi > 0 && iscontp(s + posi)) posi--;
}
else {
} else {
if (iscontp(s + posi))
return luaL_error(L, "initial position is a continuation byte");
if (n < 0) {
@ -206,8 +205,7 @@ static int byteoffset (lua_State *L) {
} while (posi > 0 && iscontp(s + posi));
n++;
}
}
else {
} else {
n--; /* do not move for 1st character */
while (n > 0 && posi < (lua_Integer)len) {
do { /* find beginning of next character */
@ -225,7 +223,7 @@ static int byteoffset (lua_State *L) {
}
static int iter_aux (lua_State *L, int strict) {
static int iter_aux(lua_State *L, int strict) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
@ -246,16 +244,16 @@ static int iter_aux (lua_State *L, int strict) {
}
static int iter_auxstrict (lua_State *L) {
static int iter_auxstrict(lua_State *L) {
return iter_aux(L, 1);
}
static int iter_auxlax (lua_State *L) {
static int iter_auxlax(lua_State *L) {
return iter_aux(L, 0);
}
static int iter_codes (lua_State *L) {
static int iter_codes(lua_State *L) {
int lax = lua_toboolean(L, 2);
const char *s = luaL_checkstring(L, 1);
luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
@ -282,9 +280,9 @@ static const luaL_Reg funcs[] = {
};
LUAMOD_API int luaopen_utf8 (lua_State *L) {
LUAMOD_API int luaopen_utf8(lua_State *L) {
luaL_newlib(L, funcs);
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT) / sizeof(char) - 1);
lua_setfield(L, -2, "charpattern");
return 1;
}

View file

@ -87,7 +87,7 @@
** are disabled via macro 'cvt2num'), do not modify 'result'
** and return 0.
*/
static int l_strton (const TValue *obj, TValue *result) {
static int l_strton(const TValue *obj, TValue *result) {
lua_assert(obj != result);
if (!cvt2num(obj)) /* is object not a string? */
return 0;
@ -102,17 +102,15 @@ static int l_strton (const TValue *obj, TValue *result) {
** Try to convert a value to a float. The float case is already handled
** by the macro 'tonumber'.
*/
int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
int luaV_tonumber_(const TValue *obj, lua_Number *n) {
TValue v;
if (ttisinteger(obj)) {
*n = cast_num(ivalue(obj));
return 1;
}
else if (l_strton(obj, &v)) { /* string coercible to number? */
} else if (l_strton(obj, &v)) { /* string coercible to number? */
*n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
return 1;
}
else
} else
return 0; /* conversion failed */
}
@ -120,7 +118,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
/*
** try to convert a float to an integer, rounding according to 'mode'.
*/
int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod 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 == F2Ieq) return 0; /* fails if mode demands integral value */
@ -136,14 +134,13 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
** without string coercion.
** ("Fast track" handled by macro 'tointegerns'.)
*/
int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod 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)) {
*p = ivalue(obj);
return 1;
}
else
} else
return 0;
}
@ -151,7 +148,7 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
/*
** try to convert a value to an integer.
*/
int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
int luaV_tointeger(const TValue *obj, lua_Integer *p, F2Imod mode) {
TValue v;
if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
obj = &v; /* change it to point to its corresponding number */
@ -175,7 +172,7 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
** 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,
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 ? F2Iceil : F2Ifloor))) {
/* not coercible to in integer */
@ -186,8 +183,7 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
if (luai_numlt(0, flim)) { /* if it is positive, it is too large */
if (step < 0) return 1; /* initial value must be less than it */
*p = LUA_MAXINTEGER; /* truncate */
}
else { /* it is less than min integer */
} else { /* it is less than min integer */
if (step > 0) return 1; /* initial value must be greater than it */
*p = LUA_MININTEGER; /* truncate */
}
@ -205,7 +201,7 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
** ra + 2 : step
** ra + 3 : control variable
*/
static int forprep (lua_State *L, StkId ra) {
static int forprep(lua_State *L, StkId ra) {
TValue *pinit = s2v(ra);
TValue *plimit = s2v(ra + 1);
TValue *pstep = s2v(ra + 2);
@ -224,8 +220,7 @@ static int forprep (lua_State *L, StkId ra) {
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 */
} else { /* step < 0; descending loop */
count = l_castS2U(init) - l_castS2U(limit);
/* 'step+1' avoids negating 'mininteger' */
count /= l_castS2U(-(step + 1)) + 1u;
@ -234,9 +229,10 @@ static int forprep (lua_State *L, StkId ra) {
needed anymore) */
setivalue(plimit, l_castU2S(count));
}
}
else { /* try making all values floats */
lua_Number init; lua_Number limit; lua_Number step;
} else { /* try making all values floats */
lua_Number init;
lua_Number limit;
lua_Number step;
if (l_unlikely(!tonumber(plimit, &limit)))
luaG_forerror(L, plimit, "limit");
if (l_unlikely(!tonumber(pstep, &step)))
@ -265,7 +261,7 @@ static int forprep (lua_State *L, StkId ra) {
** true iff the loop must continue. (The integer case is
** written online with opcode OP_FORLOOP, for performance.)
*/
static int floatforloop (StkId ra) {
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 */
@ -275,8 +271,7 @@ static int floatforloop (StkId ra) {
chgfltvalue(s2v(ra), idx); /* update internal index */
setfltvalue(s2v(ra + 3), idx); /* and control variable */
return 1; /* jump back */
}
else
} else
return 0; /* finish the loop */
}
@ -286,7 +281,7 @@ static int floatforloop (StkId ra) {
** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to
** t[k] entry (which must be empty).
*/
void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
void luaV_finishget(lua_State *L, const TValue *t, TValue *key, StkId val,
const TValue *slot) {
int loop; /* counter to avoid infinite loops */
const TValue *tm; /* metamethod */
@ -297,8 +292,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
if (l_unlikely(notm(tm)))
luaG_typeerror(L, t, "index"); /* no metamethod */
/* else will try the metamethod */
}
else { /* 't' is a table */
} else { /* 't' is a table */
lua_assert(isempty(slot));
tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
if (tm == NULL) { /* no metamethod? */
@ -329,7 +323,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
** is no such entry. (The value at 'slot' must be empty, otherwise
** 'luaV_fastget' would have done the job.)
*/
void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
void luaV_finishset(lua_State *L, const TValue *t, TValue *key,
TValue *val, const TValue *slot) {
int loop; /* counter to avoid infinite loops */
for (loop = 0; loop < MAXTAGLOOP; loop++) {
@ -345,8 +339,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
return;
}
/* else will try the metamethod */
}
else { /* not a table; check metamethod */
} else { /* not a table; check metamethod */
tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
if (l_unlikely(notm(tm)))
luaG_typeerror(L, t, "index");
@ -375,7 +368,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
** of the strings. Note that segments can compare equal but still
** have different lengths.
*/
static int l_strcmp (const TString *ts1, const TString *ts2) {
static int l_strcmp(const TString *ts1, const TString *ts2) {
const char *s1 = getstr(ts1);
size_t rl1 = tsslen(ts1); /* real length */
const char *s2 = getstr(ts2);
@ -392,8 +385,12 @@ static int l_strcmp (const TString *ts1, const TString *ts2) {
else if (zl1 == rl1) /* 's1' is finished? */
return -1; /* 's1' is less than 's2' ('s2' is not finished) */
/* both strings longer than 'zl'; go on comparing after the '\0' */
zl1++; zl2++;
s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
zl1++;
zl2++;
s1 += zl1;
rl1 -= zl1;
s2 += zl2;
rl2 -= zl2;
}
}
}
@ -410,7 +407,7 @@ static int l_strcmp (const TString *ts1, const TString *ts2) {
** from float to int.)
** When 'f' is NaN, comparisons must result in false.
*/
l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
l_sinline int LTintfloat(lua_Integer i, lua_Number f) {
if (l_intfitsf(i))
return luai_numlt(cast_num(i), f); /* compare them as floats */
else { /* i < f <=> i < ceil(f) */
@ -427,7 +424,7 @@ l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
** Check whether integer 'i' is less than or equal to float 'f'.
** See comments on previous function.
*/
l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
l_sinline int LEintfloat(lua_Integer i, lua_Number f) {
if (l_intfitsf(i))
return luai_numle(cast_num(i), f); /* compare them as floats */
else { /* i <= f <=> i <= floor(f) */
@ -444,7 +441,7 @@ l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
** Check whether float 'f' is less than integer 'i'.
** See comments on previous function.
*/
l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
l_sinline int LTfloatint(lua_Number f, lua_Integer i) {
if (l_intfitsf(i))
return luai_numlt(f, cast_num(i)); /* compare them as floats */
else { /* f < i <=> floor(f) < i */
@ -461,7 +458,7 @@ l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
** Check whether float 'f' is less than or equal to integer 'i'.
** See comments on previous function.
*/
l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
l_sinline int LEfloatint(lua_Number f, lua_Integer i) {
if (l_intfitsf(i))
return luai_numle(f, cast_num(i)); /* compare them as floats */
else { /* f <= i <=> ceil(f) <= i */
@ -477,7 +474,7 @@ l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
/*
** Return 'l < r', for numbers.
*/
l_sinline int LTnum (const TValue *l, const TValue *r) {
l_sinline int LTnum(const TValue *l, const TValue *r) {
lua_assert(ttisnumber(l) && ttisnumber(r));
if (ttisinteger(l)) {
lua_Integer li = ivalue(l);
@ -485,8 +482,7 @@ l_sinline int LTnum (const TValue *l, const TValue *r) {
return li < ivalue(r); /* both are integers */
else /* 'l' is int and 'r' is float */
return LTintfloat(li, fltvalue(r)); /* l < r ? */
}
else {
} else {
lua_Number lf = fltvalue(l); /* 'l' must be float */
if (ttisfloat(r))
return luai_numlt(lf, fltvalue(r)); /* both are float */
@ -499,7 +495,7 @@ l_sinline int LTnum (const TValue *l, const TValue *r) {
/*
** Return 'l <= r', for numbers.
*/
l_sinline int LEnum (const TValue *l, const TValue *r) {
l_sinline int LEnum(const TValue *l, const TValue *r) {
lua_assert(ttisnumber(l) && ttisnumber(r));
if (ttisinteger(l)) {
lua_Integer li = ivalue(l);
@ -507,8 +503,7 @@ l_sinline int LEnum (const TValue *l, const TValue *r) {
return li <= ivalue(r); /* both are integers */
else /* 'l' is int and 'r' is float */
return LEintfloat(li, fltvalue(r)); /* l <= r ? */
}
else {
} else {
lua_Number lf = fltvalue(l); /* 'l' must be float */
if (ttisfloat(r))
return luai_numle(lf, fltvalue(r)); /* both are float */
@ -521,7 +516,7 @@ l_sinline int LEnum (const TValue *l, const TValue *r) {
/*
** return 'l < r' for non-numbers.
*/
static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
static int lessthanothers(lua_State *L, const TValue *l, const TValue *r) {
lua_assert(!ttisnumber(l) || !ttisnumber(r));
if (ttisstring(l) && ttisstring(r)) /* both are strings? */
return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
@ -533,7 +528,7 @@ static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
/*
** Main operation less than; return 'l < r'.
*/
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
int luaV_lessthan(lua_State *L, const TValue *l, const TValue *r) {
if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
return LTnum(l, r);
else return lessthanothers(L, l, r);
@ -543,7 +538,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
/*
** return 'l <= r' for non-numbers.
*/
static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
static int lessequalothers(lua_State *L, const TValue *l, const TValue *r) {
lua_assert(!ttisnumber(l) || !ttisnumber(r));
if (ttisstring(l) && ttisstring(r)) /* both are strings? */
return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
@ -555,7 +550,7 @@ static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
/*
** Main operation less than or equal to; return 'l <= r'.
*/
int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
int luaV_lessequal(lua_State *L, const TValue *l, const TValue *r) {
if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
return LEnum(l, r);
else return lessequalothers(L, l, r);
@ -566,7 +561,7 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
** Main operation for equality of Lua values; return 't1 == t2'.
** L == NULL means raw equality (no metamethods)
*/
int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
int luaV_equalobj(lua_State *L, const TValue *t1, const TValue *t2) {
const TValue *tm;
if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */
if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
@ -583,13 +578,22 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
}
/* values have same type and same variant */
switch (ttypetag(t1)) {
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_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;
@ -625,7 +629,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
/* copy strings in stack from top - n up to top - 1 to buffer */
static void copy2buff (StkId top, int n, char *buff) {
static void copy2buff(StkId top, int n, char *buff) {
size_t tl = 0; /* size already copied */
do {
TString *st = tsvalue(s2v(top - n));
@ -640,7 +644,7 @@ static void copy2buff (StkId top, int n, char *buff) {
** Main operation for concatenation: concat 'total' values in the stack,
** from 'L->top.p - total' up to 'L->top.p - 1'.
*/
void luaV_concat (lua_State *L, int total) {
void luaV_concat(lua_State *L, int total) {
if (total == 1)
return; /* "all" values already concatenated */
do {
@ -653,8 +657,7 @@ void luaV_concat (lua_State *L, int total) {
cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
setobjs2s(L, top - 2, top - 1); /* result is second op. */
}
else {
} else {
/* at least two non-empty string values; get as many as possible */
size_t tl = tsslen(tsvalue(s2v(top - 1)));
TString *ts;
@ -671,8 +674,7 @@ void luaV_concat (lua_State *L, int total) {
char buff[LUAI_MAXSHORTLEN];
copy2buff(top, n, buff); /* copy strings to buffer */
ts = luaS_newlstr(L, buff, tl);
}
else { /* long string; copy strings directly to final result */
} else { /* long string; copy strings directly to final result */
ts = luaS_createlngstrobj(L, tl);
copy2buff(top, n, getlngstr(ts));
}
@ -687,7 +689,7 @@ void luaV_concat (lua_State *L, int total) {
/*
** Main operation 'ra = #rb'.
*/
void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
void luaV_objlen(lua_State *L, StkId ra, const TValue *rb) {
const TValue *tm;
switch (ttypetag(rb)) {
case LUA_VTABLE: {
@ -722,13 +724,12 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
** otherwise 'floor(q) == trunc(q) - 1'.
*/
lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
lua_Integer luaV_idiv(lua_State *L, lua_Integer m, lua_Integer n) {
if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
if (n == 0)
luaG_runerror(L, "attempt to divide by zero");
return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
}
else {
} else {
lua_Integer q = m / n; /* perform C division */
if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
q -= 1; /* correct result for different rounding */
@ -742,13 +743,12 @@ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
** negative operands follows C99 behavior. See previous comment
** about luaV_idiv.)
*/
lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
lua_Integer luaV_mod(lua_State *L, lua_Integer m, lua_Integer n) {
if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
if (n == 0)
luaG_runerror(L, "attempt to perform 'n%%0'");
return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
}
else {
} else {
lua_Integer r = m % n;
if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */
r += n; /* correct result for different rounding */
@ -760,7 +760,7 @@ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
/*
** Float modulus
*/
lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
lua_Number luaV_modf(lua_State *L, lua_Number m, lua_Number n) {
lua_Number r;
luai_nummod(L, m, n, r);
return r;
@ -774,14 +774,13 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
/*
** Shift left operation. (Shift right just negates 'y'.)
*/
lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
lua_Integer luaV_shiftl(lua_Integer x, lua_Integer y) {
if (y < 0) { /* shift right? */
if (y <= -NBITS) return 0;
else return intop(>>, x, -y);
}
else { /* shift left */
else return intop( >>, x, -y);
} else { /* shift left */
if (y >= NBITS) return 0;
else return intop(<<, x, y);
else return intop( <<, x, y);
}
}
@ -790,7 +789,7 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
** create a new Lua closure, push it in the stack, and initialize
** its upvalues.
*/
static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
static void pushclosure(lua_State *L, Proto *p, UpVal **encup, StkId base,
StkId ra) {
int nup = p->sizeupvalues;
Upvaldesc *uv = p->upvalues;
@ -811,25 +810,35 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
/*
** finish execution of an opcode interrupted by a yield
*/
void luaV_finishOp (lua_State *L) {
void luaV_finishOp(lua_State *L) {
CallInfo *ci = L->ci;
StkId base = ci->func.p + 1;
Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
OpCode op = GET_OPCODE(inst);
switch (op) { /* finish its execution */
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
case OP_MMBIN:
case OP_MMBINI:
case OP_MMBINK: {
setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p);
break;
}
case OP_UNM: case OP_BNOT: case OP_LEN:
case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
case OP_GETFIELD: case OP_SELF: {
case OP_UNM:
case OP_BNOT:
case OP_LEN:
case OP_GETTABUP:
case OP_GETTABLE:
case OP_GETI:
case OP_GETFIELD:
case OP_SELF: {
setobjs2s(L, base + GETARG_A(inst), --L->top.p);
break;
}
case OP_LT: case OP_LE:
case OP_LTI: case OP_LEI:
case OP_GTI: case OP_GEI:
case OP_LT:
case OP_LE:
case OP_LTI:
case OP_LEI:
case OP_GTI:
case OP_GEI:
case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
int res = !l_isfalse(s2v(L->top.p - 1));
L->top.p--;
@ -1148,7 +1157,7 @@ void luaV_finishOp (lua_State *L) {
#define vmbreak break
void luaV_execute (lua_State *L, CallInfo *ci) {
void luaV_execute(lua_State *L, CallInfo *ci) {
LClosure *cl;
TValue *k;
StkId base;
@ -1157,9 +1166,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
#if LUA_USE_JUMPTABLE
#include "ljumptab.h"
#endif
startfunc:
startfunc:
trap = L->hookmask;
returning: /* trap already set */
returning: /* trap already set */
cl = ci_func(ci);
k = cl->p->k;
pc = ci->u.l.savedpc;
@ -1170,15 +1179,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
for (;;) {
Instruction i; /* instruction being executed */
vmfetch();
#if 0
#if 0
/* low-level line tracing for debugging Lua */
printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p)));
#endif
#endif
lua_assert(base == ci->func.p + 1);
lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
/* invalidate top for instructions not expecting it */
lua_assert(isIT(i) || (cast_void(L->top.p = base), 1));
vmdispatch (GET_OPCODE(i)) {
vmdispatch(GET_OPCODE(i)) {
vmcase(OP_MOVE) {
StkId ra = RA(i);
setobjs2s(L, ra, RB(i));
@ -1205,7 +1214,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
vmcase(OP_LOADKX) {
StkId ra = RA(i);
TValue *rb;
rb = k + GETARG_Ax(*pc); pc++;
rb = k + GETARG_Ax(*pc);
pc++;
setobj2s(L, ra, rb);
vmbreak;
}
@ -1254,8 +1264,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TString *key = tsvalue(rc); /* key must be a short string */
if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
setobj2s(L, ra, slot);
}
else
} else
Protect(luaV_finishget(L, upval, rc, ra, slot));
vmbreak;
}
@ -1269,8 +1278,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot))
: luaV_fastget(L, rb, rc, slot, luaH_get)) {
setobj2s(L, ra, slot);
}
else
} else
Protect(luaV_finishget(L, rb, rc, ra, slot));
vmbreak;
}
@ -1281,8 +1289,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
int c = GETARG_C(i);
if (luaV_fastgeti(L, rb, c, slot)) {
setobj2s(L, ra, slot);
}
else {
} else {
TValue key;
setivalue(&key, c);
Protect(luaV_finishget(L, rb, &key, ra, slot));
@ -1297,8 +1304,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TString *key = tsvalue(rc); /* key must be a short string */
if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
setobj2s(L, ra, slot);
}
else
} else
Protect(luaV_finishget(L, rb, rc, ra, slot));
vmbreak;
}
@ -1310,8 +1316,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TString *key = tsvalue(rb); /* key must be a short string */
if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
luaV_finishfastset(L, upval, slot, rc);
}
else
} else
Protect(luaV_finishset(L, upval, rb, rc, slot));
vmbreak;
}
@ -1325,8 +1330,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot))
: luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
luaV_finishfastset(L, s2v(ra), slot, rc);
}
else
} else
Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
vmbreak;
}
@ -1337,8 +1341,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TValue *rc = RKC(i);
if (luaV_fastgeti(L, s2v(ra), c, slot)) {
luaV_finishfastset(L, s2v(ra), slot, rc);
}
else {
} else {
TValue key;
setivalue(&key, c);
Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
@ -1353,8 +1356,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TString *key = tsvalue(rb); /* key must be a short string */
if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
luaV_finishfastset(L, s2v(ra), slot, rc);
}
else
} else
Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
vmbreak;
}
@ -1386,8 +1388,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
setobj2s(L, ra + 1, rb);
if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
setobj2s(L, ra, slot);
}
else
} else
Protect(luaV_finishget(L, rb, rc, ra, slot));
vmbreak;
}
@ -1443,7 +1444,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
int ic = GETARG_sC(i);
lua_Integer ib;
if (tointegerns(rb, &ib)) {
pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
pc++;
setivalue(s2v(ra), luaV_shiftl(ib, -ic));
}
vmbreak;
}
@ -1453,7 +1455,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
int ic = GETARG_sC(i);
lua_Integer ib;
if (tointegerns(rb, &ib)) {
pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
pc++;
setivalue(s2v(ra), luaV_shiftl(ic, ib));
}
vmbreak;
}
@ -1544,11 +1547,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
if (ttisinteger(rb)) {
lua_Integer ib = ivalue(rb);
setivalue(s2v(ra), intop(-, 0, ib));
}
else if (tonumberns(rb, nb)) {
} else if (tonumberns(rb, nb)) {
setfltvalue(s2v(ra), luai_numunm(L, nb));
}
else
} else
Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
vmbreak;
}
@ -1558,8 +1559,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
lua_Integer ib;
if (tointegerns(rb, &ib)) {
setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
}
else
} else
Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
vmbreak;
}
@ -1742,8 +1742,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
savepc(ci);
luaD_poscall(L, ci, 0); /* no hurry... */
trap = 1;
}
else { /* do the 'poscall' here */
} else { /* do the 'poscall' here */
int nres;
L->ci = ci->previous; /* back to caller */
L->top.p = base - 1;
@ -1759,8 +1758,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
savepc(ci);
luaD_poscall(L, ci, 1); /* no hurry... */
trap = 1;
}
else { /* do the 'poscall' here */
} else { /* do the 'poscall' here */
int nres = ci->nresults;
L->ci = ci->previous; /* back to caller */
if (nres == 0)
@ -1773,7 +1771,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
setnilvalue(s2v(L->top.p++)); /* complete missing results */
}
}
ret: /* return from a Lua function */
ret: /* return from a Lua function */
if (ci->callstatus & CIST_FRESH)
return; /* end this frame */
else {
@ -1794,8 +1792,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
setivalue(s2v(ra + 3), idx); /* and control variable */
pc -= GETARG_Bx(i); /* jump back */
}
}
else if (floatforloop(ra)) /* float loop */
} else if (floatforloop(ra)) /* float loop */
pc -= GETARG_Bx(i); /* jump back */
updatetrap(ci); /* allows a signal to break the loop */
vmbreak;
@ -1817,7 +1814,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
goto l_tforcall;
}
vmcase(OP_TFORCALL) {
l_tforcall: {
l_tforcall: {
StkId ra = RA(i);
/* 'ra' has the iterator function, 'ra + 1' has the state,
'ra + 2' has the control variable, and 'ra + 3' has the
@ -1832,16 +1829,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
i = *(pc++); /* go to next instruction */
lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
goto l_tforloop;
}}
}
}
vmcase(OP_TFORLOOP) {
l_tforloop: {
l_tforloop: {
StkId ra = RA(i);
if (!ttisnil(s2v(ra + 4))) { /* continue loop? */
setobjs2s(L, ra + 2, ra + 4); /* save control variable */
pc -= GETARG_Bx(i); /* jump back */
}
vmbreak;
}}
}
}
vmcase(OP_SETLIST) {
StkId ra = RA(i);
int n = GETARG_B(i);

View file

@ -117,25 +117,25 @@ typedef enum {
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, F2Imod mode);
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
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, 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,
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,
LUAI_FUNC void luaV_finishset(lua_State *L, const TValue *t, TValue *key,
TValue *val, const TValue *slot);
LUAI_FUNC void luaV_finishOp (lua_State *L);
LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci);
LUAI_FUNC void luaV_concat (lua_State *L, int total);
LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y);
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
LUAI_FUNC void luaV_finishOp(lua_State *L);
LUAI_FUNC void luaV_execute(lua_State *L, CallInfo *ci);
LUAI_FUNC void luaV_concat(lua_State *L, int total);
LUAI_FUNC lua_Integer luaV_idiv(lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_mod(lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Number luaV_modf(lua_State *L, lua_Number x, lua_Number y);
LUAI_FUNC lua_Integer luaV_shiftl(lua_Integer x, lua_Integer y);
LUAI_FUNC void luaV_objlen(lua_State *L, StkId ra, const TValue *rb);
#endif

View file

@ -20,7 +20,7 @@
#include "lzio.h"
int luaZ_fill (ZIO *z) {
int luaZ_fill(ZIO *z) {
size_t size;
lua_State *L = z->L;
const char *buff;
@ -35,7 +35,7 @@ int luaZ_fill (ZIO *z) {
}
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, void *data) {
z->L = L;
z->reader = reader;
z->data = data;
@ -45,7 +45,7 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
/* --------------------------------------------------------------- read --- */
size_t luaZ_read (ZIO *z, void *b, size_t n) {
size_t luaZ_read(ZIO *z, void *b, size_t n) {
while (n) {
size_t m;
if (z->n == 0) { /* no bytes in buffer? */

View file

@ -44,9 +44,9 @@ typedef struct Mbuffer {
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
LUAI_FUNC void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader,
void *data);
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
LUAI_FUNC size_t luaZ_read(ZIO *z, void *b, size_t n); /* read next n bytes */
@ -61,6 +61,6 @@ struct Zio {
};
LUAI_FUNC int luaZ_fill (ZIO *z);
LUAI_FUNC int luaZ_fill(ZIO *z);
#endif

View file

@ -3854,11 +3854,11 @@ static int iclass_recover(uint8_t key[8], uint32_t index_start, uint32_t loop, u
int runs = 1;
int cycle = 1;
bool repeat = true;
if(allnight){
if (allnight) {
runs = 10;
}
while (repeat == true){
while (repeat == true) {
uint32_t payload_size = sizeof(iclass_recover_req_t);
uint8_t aa2_standard_key[PICOPASS_BLOCK_SIZE] = {0};
memcpy(aa2_standard_key, iClass_Key_Table[1], PICOPASS_BLOCK_SIZE);
@ -3896,23 +3896,23 @@ static int iclass_recover(uint8_t key[8], uint32_t index_start, uint32_t loop, u
if (resp.status == PM3_SUCCESS) {
PrintAndLogEx(SUCCESS, "iCLASS Key Bits Recovery: " _GREEN_("completed!"));
repeat = false;
} else if (resp.status == PM3_ESOFT){
} else if (resp.status == PM3_ESOFT) {
PrintAndLogEx(WARNING, "iCLASS Key Bits Recovery: " _RED_("failed/errors"));
repeat = false;
} else if (resp.status == PM3_EINVARG){
if(allnight){
if(runs <= cycle){
} else if (resp.status == PM3_EINVARG) {
if (allnight) {
if (runs <= cycle) {
repeat = false;
}else{
index_start = index_start+loop;
} else {
index_start = index_start + loop;
cycle++;
}
}else{
} else {
repeat = false;
}
}
free(payload);
if(!repeat){
if (!repeat) {
return resp.status;
}
}
@ -4052,16 +4052,16 @@ static int CmdHFiClassLegRecLookUp(const char *Cmd) {
check_values = false;
}
}
if(check_values){
if (check_values) {
PrintAndLogEx(SUCCESS, _GREEN_("CONFIRMED VALID RAW key ") _RED_("%s"), sprint_hex(div_key, 8));
verified = true;
}else{
} else {
PrintAndLogEx(INFO, _YELLOW_("Raw Key Invalid"));
}
}
if(index % 1000000 == 0){
PrintAndLogEx(INFO, "Tested: " _YELLOW_("%" PRIu64 )" million keys", index/1000000);
if (index % 1000000 == 0) {
PrintAndLogEx(INFO, "Tested: " _YELLOW_("%" PRIu64)" million keys", index / 1000000);
PrintAndLogEx(INFO, "Last Generated Key Value: " _YELLOW_("%s"), sprint_hex(div_key, 8));
}
index++;
@ -4104,15 +4104,15 @@ static int CmdHFiClassLegacyRecover(const char *Cmd) {
bool no_test = arg_get_lit(ctx, 5);
bool allnight = arg_get_lit(ctx, 6);
if(no_test){
test=false;
if (no_test) {
test = false;
}
if(loop > 10000){
if (loop > 10000) {
PrintAndLogEx(ERR, "Too many loops, arm prone to crashes. For safety specify a number lower than 10000");
CLIParserFree(ctx);
return PM3_EINVARG;
}else if (debug || test){
} else if (debug || test) {
loop = 1;
}
@ -4124,7 +4124,7 @@ static int CmdHFiClassLegacyRecover(const char *Cmd) {
return PM3_ESOFT;
}
diversifyKey(csn, iClass_Key_Table[1], new_div_key);
memcpy(no_first_auth,new_div_key,PICOPASS_BLOCK_SIZE);
memcpy(no_first_auth, new_div_key, PICOPASS_BLOCK_SIZE);
CLIParserFree(ctx);
@ -4133,7 +4133,7 @@ static int CmdHFiClassLegacyRecover(const char *Cmd) {
return PM3_EINVARG;
}
iclass_recover(macs,index,loop,no_first_auth,debug,test,allnight);
iclass_recover(macs, index, loop, no_first_auth, debug, test, allnight);
PrintAndLogEx(WARNING, _YELLOW_("If the process completed successfully, you can now run 'hf iclass legbrute' with the partial key found."));

View file

@ -363,7 +363,8 @@ static int CmdLFHitagSSim(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_lit0("8", "82xx", "simulate 8268/8310"),
arg_param_end};
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
// bool use_82xx = arg_get_lit(ctx, 1); // not implemented yet

View file

@ -240,8 +240,8 @@ static uint64_t reverse_check(uint64_t z) {
uint64_t ck1 = z & ~(ck2 >> 24); // Clear the bits where ck2 affected the result
// Now ck1 and ck2 have their original values before (after ck function took place)
ck1 = reverse_ck(3,2,ck1);
ck2 = reverse_ck(3,2,ck2);
ck1 = reverse_ck(3, 2, ck1);
ck2 = reverse_ck(3, 2, ck2);
return ck1 | ck2 >> 24; //This is now zP
@ -289,10 +289,10 @@ static void reverse_permute(BitstreamIn_t *p_in, uint64_t z, int l, BitstreamOut
if (pn) { //if p == 1 for that six bit position, then sum it
// pn = 1
uint8_t zl = getSixBitByte(z, l);
if(fix){
if (fix) {
push6bits(out1, zl - 1);
}else{
push6bits(out1, zl );
} else {
push6bits(out1, zl);
}
} else {
// otherwise
@ -424,25 +424,25 @@ void invert_hash0(uint8_t k[8]) {
for (int i = 0; i < 8; i++) {
y |= ((k[i] & 0x80) >> (7 - i)); // Recover the bit of y from the leftmost bit of k[i]
pushbackSixBitByte(&zTilde, (k[i] & 0x7E) >> 1, i); // Recover the six bits of zTilde from the middle of k[i]
if(g_debugMode > 0)printState("z~", zTilde);
if (g_debugMode > 0)printState("z~", zTilde);
p |= ((k[i] & 0x01) << i);
}
if(g_debugMode > 0)PrintAndLogEx(INFO, " y : %02x", y); //value of y (recovered 1 byte of the pre-image)
if (g_debugMode > 0)PrintAndLogEx(INFO, " y : %02x", y); //value of y (recovered 1 byte of the pre-image)
//check if p is part of the array pi, if not invert it
if(g_debugMode > 0)PrintAndLogEx(INFO, " p : %02x", p); //value of p (at some point in the original hash0)
if (g_debugMode > 0)PrintAndLogEx(INFO, " p : %02x", p); //value of p (at some point in the original hash0)
int remainder = find_p_in_pi(p);
if (remainder < 0){
if (remainder < 0) {
p = ~p;
remainder = find_p_in_pi(p);
}
if(g_debugMode > 0)PrintAndLogEx(INFO, " p or ~p : %02x", p); //value of p (at some point in the original hash0)
if (g_debugMode > 0)PrintAndLogEx(INFO, " p or ~p : %02x", p); //value of p (at some point in the original hash0)
//find possible values of x that can return the same remainder
uint8_t x_count = 0;
uint8_t x_array[8];
for (int x = 0x00; x <= 0xFF; x++) {
if(x % 35 == remainder){
if (x % 35 == remainder) {
x_array[x_count] = x;
x_count++;
}
@ -452,11 +452,11 @@ void invert_hash0(uint8_t k[8]) {
pre_image_base[1] = y;
//calculate pre-images based on the potential values of x (should we use pre-flip p and post flip p just in case?)
uint64_t zTil_img[8] = {0}; //8 is the max size it'll have as per max number of X pre-images
for(int img = 0; img < x_count; img++){ //for each potential value of x calculate a pre-image
for (int img = 0; img < x_count; img++) { //for each potential value of x calculate a pre-image
zTil_img[img] = zTilde;
pre_image_base[0] = x_array[img];
uint8_t pc = p; //redefine and reassociate it here or it'll keep changing through the loops
if (x_array[img] & 1){ //Check if potential x7 is 1, if it is then invert p
if (x_array[img] & 1) { //Check if potential x7 is 1, if it is then invert p
pc = ~p;
}
//calculate zTilde for the x preimage
@ -464,7 +464,7 @@ void invert_hash0(uint8_t k[8]) {
uint8_t p_i = (pc >> i) & 0x1; //this is correct!
uint8_t zTilde_i = getSixBitByte(zTilde, i) << 1;
if (k[i] & 0x80) { //this checks the value of the first bit of the byte (value of y_i)
if(p_i){
if (p_i) {
zTilde_i--;
}
zTilde_i = ~zTilde_i; //flip the 6 bit string
@ -475,7 +475,7 @@ void invert_hash0(uint8_t k[8]) {
pushbackSixBitByte(&zTil_img[img], zTilde_i >> 1, i);
}
if(g_debugMode > 0){
if (g_debugMode > 0) {
PrintAndLogEx(INFO, _YELLOW_("Testing Pre-Image Base: %s"), sprint_hex(pre_image_base, sizeof(pre_image_base)));
PrintAndLogEx(DEBUG, " | x| y|z0|z1|z2|z3|z4|z5|z6|z7|");
printState("0|0|z~", zTil_img[img]); //we retrieve the values of z~
@ -522,7 +522,7 @@ void invert_hash0(uint8_t k[8]) {
uint64_t c = 0;
for (int n=0; n < 4; n++){
for (int n = 0; n < 4; n++) {
uint8_t _zn = getSixBitByte(zP, n);
uint8_t _zn4 = getSixBitByte(zP, n + 4);
@ -537,17 +537,17 @@ void invert_hash0(uint8_t k[8]) {
//When these values are present we need to generate additional pre-images if they have the same modulo as other values
// Initialize an array of pointers to uint64_t (start with one value, initialized to 0)
uint64_t* hydra_heads = (uint64_t*)malloc(sizeof(uint64_t)); // Start with one uint64_t
uint64_t *hydra_heads = (uint64_t *)malloc(sizeof(uint64_t)); // Start with one uint64_t
hydra_heads[0] = 0; // Initialize first value to 0
int heads_count = 1; // Track number of forks
// Iterate 4 times as per the original loop
for (int n=0; n < 8; n++){
for (int n = 0; n < 8; n++) {
uint8_t hydra_head = getSixBitByte(c, n);
if(hydra_head <= (n % 4) || hydra_head >= 63-(n % 4)){
if (hydra_head <= (n % 4) || hydra_head >= 63 - (n % 4)) {
// Create new forks by duplicating existing uint64_t values
int new_head = heads_count * 2;
hydra_heads = (uint64_t*)realloc(hydra_heads, new_head * sizeof(uint64_t));
hydra_heads = (uint64_t *)realloc(hydra_heads, new_head * sizeof(uint64_t));
// Duplicate all current values and add the value to both original and new ones
for (int i = 0; i < heads_count; i++) {
// Duplicate current value
@ -556,19 +556,19 @@ void invert_hash0(uint8_t k[8]) {
uint8_t big_hydra_head = 0;
uint8_t hydra_lil_spawns[4] = {0x00, 0x01, 0x02, 0x03};
uint8_t hydra_big_spawns[4] = {0x3f, 0x3e, 0x3d, 0x3c};
if(hydra_head <= n % 4){ //check if is in the lower range
if (hydra_head <= n % 4) { //check if is in the lower range
//replace with big spawn in one hydra and keep small in another
small_hydra_head = hydra_head;
for(int fh = 0; fh < 4; fh++){
if(hydra_lil_spawns[fh] == hydra_head){
for (int fh = 0; fh < 4; fh++) {
if (hydra_lil_spawns[fh] == hydra_head) {
big_hydra_head = hydra_big_spawns[fh];
}
}
}else if(hydra_head >= 63-(n % 4)){ //or the higher range
} else if (hydra_head >= 63 - (n % 4)) { //or the higher range
//replace with small in one hydra and keep big in another
big_hydra_head = hydra_head;
for(int fh = 0; fh < 4; fh++){
if(hydra_big_spawns[fh] == hydra_head){
for (int fh = 0; fh < 4; fh++) {
if (hydra_big_spawns[fh] == hydra_head) {
small_hydra_head = hydra_lil_spawns[fh];
}
}
@ -579,25 +579,25 @@ void invert_hash0(uint8_t k[8]) {
}
// Update the count of total values
heads_count = new_head;
}else{ //no hydra head spawns
} else { //no hydra head spawns
for (int i = 0; i < heads_count; i++) {
pushbackSixBitByte(&hydra_heads[i], hydra_head, n);;
}
}
}
for(int i=0; i<heads_count; i++){
for (int i = 0; i < heads_count; i++) {
//restore the two most significant bytes (x and y)
hydra_heads[i] |= ((uint64_t)x_array[img] << 56);
hydra_heads[i] |= ((uint64_t)y << 48);
if (g_debugMode > 0){
if (g_debugMode > 0) {
PrintAndLogEx(DEBUG, " | x| y|z0|z1|z2|z3|z4|z5|z6|z7|");
printState("origin_r1", hydra_heads[i]);
}
//reverse the swapZbalues function to get the original six-bit byte order
uint64_t original_z = swapZvalues(hydra_heads[i]);
if (g_debugMode > 0){
if (g_debugMode > 0) {
PrintAndLogEx(DEBUG, " | x| y|z0|z1|z2|z3|z4|z5|z6|z7|");
printState("origin_r2", original_z);
PrintAndLogEx(INFO, "--------------------------");
@ -608,17 +608,17 @@ void invert_hash0(uint8_t k[8]) {
//verify result, if it matches add it to the list as a valid pre-image
bool image_match = true;
for(int v=0; v<8; v++){
if(img_div_key[v] != k[v]){ //compare against input key k
for (int v = 0; v < 8; v++) {
if (img_div_key[v] != k[v]) { //compare against input key k
image_match = false;
}
}
uint8_t des_pre_image[8] = {0};
x_num_to_bytes(original_z,sizeof(original_z),des_pre_image);
x_num_to_bytes(original_z, sizeof(original_z), des_pre_image);
if(image_match){
if (image_match) {
PrintAndLogEx(INFO, _GREEN_("Valid pre-image: ")_YELLOW_("%s"), sprint_hex(des_pre_image, sizeof(des_pre_image)));
}else if (!image_match && g_debugMode > 0){
} else if (!image_match && g_debugMode > 0) {
PrintAndLogEx(INFO, _RED_("Invalid pre-image: %s"), sprint_hex(des_pre_image, sizeof(des_pre_image)));
}
}

File diff suppressed because it is too large Load diff

View file

@ -149,6 +149,7 @@ const static vocabulary_t vocabulary[] = {
{ 0, "hf 14a cuids" },
{ 0, "hf 14a info" },
{ 0, "hf 14a sim" },
{ 0, "hf 14a simaid" },
{ 0, "hf 14a sniff" },
{ 0, "hf 14a raw" },
{ 0, "hf 14a reader" },
@ -281,6 +282,7 @@ const static vocabulary_t vocabulary[] = {
{ 1, "hf iclass lookup" },
{ 0, "hf iclass legrec" },
{ 1, "hf iclass legbrute" },
{ 1, "hf iclass unhash" },
{ 0, "hf iclass sim" },
{ 0, "hf iclass eload" },
{ 0, "hf iclass esave" },
@ -674,6 +676,7 @@ const static vocabulary_t vocabulary[] = {
{ 0, "lf hitag hts reader" },
{ 0, "lf hitag hts rdbl" },
{ 0, "lf hitag hts wrbl" },
{ 0, "lf hitag hts sim" },
{ 1, "lf idteck help" },
{ 1, "lf idteck demod" },
{ 0, "lf idteck reader" },

View file

@ -114,7 +114,7 @@ uint32_t reflect32(uint32_t v) {
// swap bytes
v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
// swap 2-byte long pairs
v = ( v >> 16 ) | ( v << 16);
v = (v >> 16) | (v << 16);
return v;
}

View file

@ -1310,7 +1310,12 @@
"notes": [
"hf 14a raw -sc 3000 -> select, crc, where 3000 == 'read block 00'",
"hf 14a raw -ak -b 7 40 -> send 7 bit byte 0x40",
"hf 14a raw --ecp -s -> send ECP before select"
"hf 14a raw --ecp -s -> send ECP before select",
"Crypto1 session example, with special auth shortcut 6xxx<key>:",
"hf 14a raw --crypto1 -skc 6000FFFFFFFFFFFF",
"hf 14a raw --crypto1 -kc 3000",
"hf 14a raw --crypto1 -kc 6007FFFFFFFFFFFF",
"hf 14a raw --crypto1 -c 3007"
],
"offline": false,
"options": [
@ -1327,9 +1332,10 @@
"--ecp Use enhanced contactless polling",
"--mag Use Apple magsafe polling",
"--topaz Use Topaz protocol to send command",
"--crypto1 Use crypto1 session",
"<hex> Raw bytes to send"
],
"usage": "hf 14a raw [-hack3rsv] [-t <ms>] [-b <dec>] [--ecp] [--mag] [--topaz] <hex> [<hex>]..."
"usage": "hf 14a raw [-hack3rsv] [-t <ms>] [-b <dec>] [--ecp] [--mag] [--topaz] [--crypto1] <hex> [<hex>]..."
},
"hf 14a reader": {
"command": "hf 14a reader",
@ -1382,6 +1388,30 @@
],
"usage": "hf 14a sim [-hxv] -t <1-12> [-u <hex>] [-n <dec>] [--sk]"
},
"hf 14a simaid": {
"command": "hf 14a simaid",
"description": "Simulate ISO/IEC 14443 type A tag with 4,7 or 10 byte UID, and filter for AID Values These AID Values can be responded to and include extra APDU commands on GetData after response",
"notes": [
"hf 14a simaid -t 3 -> MIFARE Desfire",
"hf 14a simaid -t 4 -> ISO/IEC 14443-4",
"hf 14a simaid -t 11 -> Javacard (JCOP)",
"hf 14a simaid -t 3 --aid a000000000000000000000 --response 9000 --apdu 9000 -> AID, Response and APDU",
"hf 14a simaid -t 3 --rats 05788172220101 --response 01009000 --apdu 86009000 -> Custom RATS Added",
"hf 14a simaid -t 3 --rats 05788172220101 -x -> Enumerate AID Values"
],
"offline": false,
"options": [
"-h, --help This help",
"-t, --type <1-12> Simulation type to use",
"-u, --uid <hex> <4|7|10> hex bytes UID",
"-r, --rats <hex> <0-20> hex bytes RATS",
"-a, --aid <hex> <0-100> hex bytes for AID to respond to (Default: A000000000000000000000)",
"-e, --response <hex> <0-100> hex bytes for APDU Response to AID Select (Default: 9000)",
"-p, --apdu <hex> <0-100> hex bytes for APDU Response to Get Data request after AID (Default: 9000)",
"-x, --enumerate Enumerate all AID values via returning Not Found and print them to console"
],
"usage": "hf 14a simaid [-hx] -t <1-12> [-u <hex>] [-r <hex>] [-a <hex>] [-e <hex>] [-p <hex>]"
},
"hf 14a sniff": {
"command": "hf 14a sniff",
"description": "Sniff the communication between reader and tag Use `hf 14a list` to view collected data.",
@ -3377,7 +3407,7 @@
},
"hf iclass help": {
"command": "hf iclass help",
"description": "help This help list List iclass history view Display content from tag dump file ----------- --------------------- Recovery -------------------- loclass Use loclass to perform bruteforce reader attack lookup Uses authentication trace to check for key in dictionary file legbrute Bruteforces 40 bits of a partial raw key ----------- ---------------------- Utils ---------------------- calcnewkey Calc diversified keys (blocks 3 & 4) to write new keys encode Encode binary wiegand to block 7 encrypt Encrypt given block data decrypt Decrypt given block data or tag dump file managekeys Manage keys to use with iclass commands permutekey Permute function from 'heart of darkness' paper --------------------------------------------------------------------------------------- hf iclass list available offline: yes Alias of `trace list -t iclass -c` with selected protocol data to annotate trace buffer You can load a trace from file (see `trace load -h`) or it be downloaded from device by default It accepts all other arguments of `trace list`. Note that some might not be relevant for this specific protocol",
"description": "help This help list List iclass history view Display content from tag dump file ----------- --------------------- Recovery -------------------- loclass Use loclass to perform bruteforce reader attack lookup Uses authentication trace to check for key in dictionary file legbrute Bruteforces 40 bits of a partial diversified key, provided 24 bits of the key and two valid nr-macs unhash Reverses a diversified key to retrieve hash0 pre-images after DES encryption ----------- ---------------------- Utils ---------------------- calcnewkey Calc diversified keys (blocks 3 & 4) to write new keys encode Encode binary wiegand to block 7 encrypt Encrypt given block data decrypt Decrypt given block data or tag dump file managekeys Manage keys to use with iclass commands permutekey Permute function from 'heart of darkness' paper --------------------------------------------------------------------------------------- hf iclass list available offline: yes Alias of `trace list -t iclass -c` with selected protocol data to annotate trace buffer You can load a trace from file (see `trace load -h`) or it be downloaded from device by default It accepts all other arguments of `trace list`. Note that some might not be relevant for this specific protocol",
"notes": [
"hf iclass list --frame -> show frame delay times",
"hf iclass list -1 -> use trace buffer"
@ -3413,30 +3443,37 @@
"command": "hf iclass legbrute",
"description": "This command take sniffed trace data and partial raw key and bruteforces the remaining 40 bits of the raw key.",
"notes": [
"hf iclass legbrute --csn 8D7BD711FEFF12E0 --epurse feffffffffffffff --macs 00000000BD478F76 --pk B4F12AADC5301225"
"hf iclass legbrute --epurse feffffffffffffff --macs1 1306cad9b6c24466 --macs2 f0bf905e35f97923 --pk B4F12AADC5301225"
],
"offline": true,
"options": [
"-h, --help This help",
"--csn <hex> Specify CSN as 8 hex bytes",
"--epurse <hex> Specify ePurse as 8 hex bytes",
"--macs <hex> MACs",
"--pk <hex> Partial Key"
"--macs1 <hex> MACs captured from the reader",
"--macs2 <hex> MACs captured from the reader, different than the first set (with the same csn and epurse value)",
"--pk <hex> Partial Key from legrec or starting key of keyblock from legbrute",
"--index <dec> Where to start from to retrieve the key, default 0 - value in millions e.g. 1 is 1 million"
],
"usage": "hf iclass legbrute [-h] --csn <hex> --epurse <hex> --macs <hex> --pk <hex>"
"usage": "hf iclass legbrute [-h] --epurse <hex> --macs1 <hex> --macs2 <hex> --pk <hex> [--index <dec>]"
},
"hf iclass legrec": {
"command": "hf iclass legrec",
"description": "Attempts to recover the diversified key of a specific iClass card. This may take a long time. The Card must remain be on the PM3 antenna during the whole process! This process may brick the card!",
"notes": [
"hf iclass legrec --macs 0000000089cb984b"
"hf iclass legrec --macs 0000000089cb984b",
"hf iclass legrec --macs 0000000089cb984b --index 0 --loop 100 --notest"
],
"offline": false,
"options": [
"-h, --help This help",
"--macs <hex> MACs"
"--macs <hex> AA1 Authentication MACs",
"--index <dec> Where to start from to retrieve the key, default 0",
"--loop <dec> The number of key retrieval cycles to perform, max 10000, default 100",
"--debug Re-enables tracing for debugging. Limits cycles to 1.",
"--notest Perform real writes on the card!",
"--allnight Loops the loop for 10 times, recommended loop value of 5000."
],
"usage": "hf iclass legrec [-h] --macs <hex>"
"usage": "hf iclass legrec [-h] --macs <hex> [--index <dec>] [--loop <dec>] [--debug] [--notest] [--allnight]"
},
"hf iclass loclass": {
"command": "hf iclass loclass",
@ -3617,6 +3654,19 @@
],
"usage": "hf iclass sniff [-hj]"
},
"hf iclass unhash": {
"command": "hf iclass unhash",
"description": "Reverses the hash0 function used generate iclass diversified keys after DES encryption, returning the DES crypted CSN.",
"notes": [
"hf iclass unhash --divkey B4F12AADC5301A2D"
],
"offline": true,
"options": [
"-h, --help This help",
"--divkey <hex> The card's Diversified Key value"
],
"usage": "hf iclass unhash [-h] --divkey <hex>"
},
"hf iclass view": {
"command": "hf iclass view",
"description": "Print a iCLASS tag dump file (bin/eml/json)",
@ -9730,21 +9780,22 @@
"command": "lf hitag hts rdbl",
"description": "Read Hitag S memory. Crypto mode: - key format ISK high + ISK low - default key 4F4E4D494B52 (ONMIKR) 8268/8310 password mode: - default password BBDD3399",
"notes": [
"lf hitag hts rdbl -> Hitag S/8211, plain mode",
"lf hitag hts rdbl --82xx -k BBDD3399 -> 8268/8310, password mode",
"lf hitag hts rdbl --nrar 0102030411223344 -> Hitag S, challenge mode",
"lf hitag hts rdbl --crypto -> Hitag S, crypto mode, def key",
"lf hitag hts rdbl -k 4F4E4D494B52 -> Hitag S, crypto mode"
"lf hitag hts rdbl -p 1 -> Hitag S/8211, plain mode",
"lf hitag hts rdbl -p 1 --82xx -k BBDD3399 -> 8268/8310, password mode",
"lf hitag hts rdbl -p 1 --nrar 0102030411223344 -> Hitag S, challenge mode",
"lf hitag hts rdbl -p 1 --crypto -> Hitag S, crypto mode, def key",
"lf hitag hts rdbl -p 1 -k 4F4E4D494B52 -> Hitag S, crypto mode"
],
"offline": false,
"options": [
"-h, --help This help",
"--nrar <hex> nonce / answer writer, 8 hex bytes",
"-8, --82xx 8268/8310 mode",
"--nrar <hex> nonce / answer writer, 8 hex bytes",
"--crypto crypto mode",
"-k, --key <hex> pwd or key, 4 or 6 hex bytes"
"-k, --key <hex> pwd or key, 4 or 6 hex bytes",
"-p, --page <dec> page address to read from"
],
"usage": "lf hitag hts rdbl [-h8] [--nrar <hex>] [--crypto] [-k <hex>]"
"usage": "lf hitag hts rdbl [-h8] [--nrar <hex>] [--crypto] [-k <hex>] -p <dec>"
},
"lf hitag hts reader": {
"command": "lf hitag hts reader",
@ -9760,6 +9811,20 @@
],
"usage": "lf hitag hts reader [-h@]"
},
"lf hitag hts sim": {
"command": "lf hitag hts sim",
"description": "Simulate Hitag S transponder You need to `lf hitag hts eload` first",
"notes": [
"lf hitag hts sim",
"lf hitag hts sim --82xx"
],
"offline": false,
"options": [
"-h, --help This help",
"-8, --82xx simulate 8268/8310"
],
"usage": "lf hitag hts sim [-h8]"
},
"lf hitag hts wrbl": {
"command": "lf hitag hts wrbl",
"description": "Write a page in Hitag S memory. Crypto mode: - key format ISK high + ISK low - default key 4F4E4D494B52 (ONMIKR) 8268/8310 password mode: - default password BBDD3399",
@ -9773,8 +9838,8 @@
"offline": false,
"options": [
"-h, --help This help",
"--nrar <hex> nonce / answer writer, 8 hex bytes",
"-8, --82xx 8268/8310 mode",
"--nrar <hex> nonce / answer writer, 8 hex bytes",
"--crypto crypto mode",
"-k, --key <hex> pwd or key, 4 or 6 hex bytes",
"-p, --page <dec> page address to write to",
@ -9817,13 +9882,8 @@
},
"lf hitag read": {
"command": "lf hitag read",
"description": "Read Hitag memory. It support Hitag S and Hitag 2 Password mode: - default key 4D494B52 (MIKR) Crypto mode: - key format ISK high + ISK low - default key 4F4E4D494B52 (ONMIKR)",
"description": "Read Hitag memory. It support Hitag 2 Password mode: - default key 4D494B52 (MIKR) Crypto mode: - key format ISK high + ISK low - default key 4F4E4D494B52 (ONMIKR)",
"notes": [
"lf hitag read --hts -> Hitag S, plain mode",
"lf hitag read --hts --nrar 0102030411223344 -> Hitag S, challenge mode",
"lf hitag read --hts --crypto -> Hitag S, crypto mode, def key",
"lf hitag read --hts -k 4F4E4D494B52 -> Hitag S, crypto mode",
"",
"lf hitag read --ht2 --pwd -> Hitag 2, pwd mode, def key",
"lf hitag read --ht2 -k 4D494B52 -> Hitag 2, pwd mode",
"lf hitag read --ht2 --nrar 0102030411223344 -> Hitag 2, challenge mode",
@ -9833,14 +9893,13 @@
"offline": false,
"options": [
"-h, --help This help",
"-s, --hts Hitag S",
"-2, --ht2 Hitag 2",
"--pwd password mode",
"--nrar <hex> nonce / answer writer, 8 hex bytes",
"--crypto crypto mode",
"-k, --key <hex> key, 4 or 6 hex bytes"
],
"usage": "lf hitag read [-hs2] [--pwd] [--nrar <hex>] [--crypto] [-k <hex>]"
"usage": "lf hitag read [-h2] [--pwd] [--nrar <hex>] [--crypto] [-k <hex>]"
},
"lf hitag reader": {
"command": "lf hitag reader",
@ -9866,10 +9925,9 @@
"options": [
"-h, --help This help",
"-1, --ht1 simulate Hitag 1",
"-2, --ht2 simulate Hitag 2",
"-s, --hts simulate Hitag S"
"-2, --ht2 simulate Hitag 2"
],
"usage": "lf hitag sim [-h12s]"
"usage": "lf hitag sim [-h12]"
},
"lf hitag sniff": {
"command": "lf hitag sniff",
@ -9911,13 +9969,8 @@
},
"lf hitag wrbl": {
"command": "lf hitag wrbl",
"description": "Write a page in Hitag memory. It support HitagS and Hitag 2 Password mode: - default key 4D494B52 (MIKR) Crypto mode: - key format ISK high + ISK low - default key 4F4E4D494B52 (ONMIKR)",
"description": "Write a page in Hitag memory. It support Hitag 2 Password mode: - default key 4D494B52 (MIKR) Crypto mode: - key format ISK high + ISK low - default key 4F4E4D494B52 (ONMIKR)",
"notes": [
"lf hitag wrbl --hts -p 6 -d 01020304 -> HitagS, plain mode",
"lf hitag wrbl --hts -p 6 -d 01020304 --nrar 0102030411223344 -> HitagS, challenge mode",
"lf hitag wrbl --hts -p 6 -d 01020304 --crypto -> HitagS, crypto mode, def key",
"lf hitag wrbl --hts -p 6 -d 01020304 -k 4F4E4D494B52 -> HitagS, crypto mode",
"",
"lf hitag wrbl --ht2 -p 6 -d 01020304 --pwd -> Hitag 2, pwd mode, def key",
"lf hitag wrbl --ht2 -p 6 -d 01020304 -k 4D494B52 -> Hitag 2, pwd mode",
"lf hitag wrbl --ht2 -p 6 -d 01020304 --nrar 0102030411223344 -> Hitag 2, challenge mode",
@ -9927,7 +9980,6 @@
"offline": false,
"options": [
"-h, --help This help",
"-s, --hts Hitag S",
"-2, --ht2 Hitag 2",
"--pwd password mode",
"--nrar <hex> nonce / answer writer, 8 hex bytes",
@ -9936,7 +9988,7 @@
"-p, --page <dec> page address to write to",
"-d, --data <hex> data, 4 hex bytes"
],
"usage": "lf hitag wrbl [-hs2] [--pwd] [--nrar <hex>] [--crypto] [-k <hex>] -p <dec> -d <hex>"
"usage": "lf hitag wrbl [-h2] [--pwd] [--nrar <hex>] [--crypto] [-k <hex>] -p <dec> -d <hex>"
},
"lf idteck clone": {
"command": "lf idteck clone",
@ -12902,8 +12954,8 @@
}
},
"metadata": {
"commands_extracted": 745,
"commands_extracted": 747,
"extracted_by": "PM3Help2JSON v1.00",
"extracted_on": "2024-09-15T16:16:09"
"extracted_on": "2024-09-30T08:35:18"
}
}

View file

@ -192,6 +192,7 @@ Check column "offline" for their availability.
|`hf 14a cuids `|N |`Collect n>0 ISO14443-a UIDs in one go`
|`hf 14a info `|N |`Tag information`
|`hf 14a sim `|N |`Simulate ISO 14443-a tag`
|`hf 14a simaid `|N |`Simulate ISO 14443-a AID Selection`
|`hf 14a sniff `|N |`sniff ISO 14443-a traffic`
|`hf 14a raw `|N |`Send raw hex data to tag`
|`hf 14a reader `|N |`Act like an ISO14443-a reader`
@ -402,8 +403,9 @@ Check column "offline" for their availability.
|`hf iclass chk `|N |`Check keys`
|`hf iclass loclass `|Y |`Use loclass to perform bruteforce reader attack`
|`hf iclass lookup `|Y |`Uses authentication trace to check for key in dictionary file`
|`hf iclass legrec `|N |`Attempts to recover the standard key of a legacy card`
|`hf iclass legbrute `|Y |`Bruteforces 40 bits of a partial raw key`
|`hf iclass legrec `|N |`Recovers 24 bits of the diversified key of a legacy card provided a valid nr-mac combination`
|`hf iclass legbrute `|Y |`Bruteforces 40 bits of a partial diversified key, provided 24 bits of the key and two valid nr-macs`
|`hf iclass unhash `|Y |`Reverses a diversified key to retrieve hash0 pre-images after DES encryption`
|`hf iclass sim `|N |`Simulate iCLASS tag`
|`hf iclass eload `|N |`Upload file into emulator memory`
|`hf iclass esave `|N |`Save emulator memory to file`
@ -1078,6 +1080,7 @@ Check column "offline" for their availability.
|`lf hitag hts reader `|N |`Act like a Hitag S reader`
|`lf hitag hts rdbl `|N |`Read Hitag S memory`
|`lf hitag hts wrbl `|N |`Write Hitag S page`
|`lf hitag hts sim `|N |`Simulate Hitag transponder`
### lf idteck