mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 10:37:23 -07:00
changing {} style to match majority of previous style
This commit is contained in:
parent
da6cdf014b
commit
961d929f4d
320 changed files with 5502 additions and 10485 deletions
252
liblua/lapi.c
252
liblua/lapi.c
|
@ -52,8 +52,7 @@ const char lua_ident[] =
|
|||
api_check(L, isstackindex(i, o), "index not in the stack")
|
||||
|
||||
|
||||
static TValue *index2addr(lua_State *L, int idx)
|
||||
{
|
||||
static TValue *index2addr(lua_State *L, int idx) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (idx > 0) {
|
||||
TValue *o = ci->func + idx;
|
||||
|
@ -82,15 +81,13 @@ static TValue *index2addr(lua_State *L, int idx)
|
|||
** to be called by 'lua_checkstack' in protected mode, to grow stack
|
||||
** capturing memory errors
|
||||
*/
|
||||
static void growstack(lua_State *L, void *ud)
|
||||
{
|
||||
static void growstack(lua_State *L, void *ud) {
|
||||
int size = *(int *)ud;
|
||||
luaD_growstack(L, size);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_checkstack(lua_State *L, int size)
|
||||
{
|
||||
LUA_API int lua_checkstack(lua_State *L, int size) {
|
||||
int res;
|
||||
CallInfo *ci = L->ci;
|
||||
lua_lock(L);
|
||||
|
@ -110,8 +107,7 @@ LUA_API int lua_checkstack(lua_State *L, int size)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -126,8 +122,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;
|
||||
|
@ -137,8 +132,7 @@ LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
|
|||
}
|
||||
|
||||
|
||||
LUA_API const lua_Number *lua_version(lua_State *L)
|
||||
{
|
||||
LUA_API const lua_Number *lua_version(lua_State *L) {
|
||||
static const lua_Number version = LUA_VERSION_NUM;
|
||||
if (L == NULL) return &version;
|
||||
else return G(L)->version;
|
||||
|
@ -154,22 +148,19 @@ LUA_API const 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 - L->ci->func + idx);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_gettop(lua_State *L)
|
||||
{
|
||||
LUA_API int lua_gettop(lua_State *L) {
|
||||
return cast_int(L->top - (L->ci->func + 1));
|
||||
}
|
||||
|
||||
|
||||
LUA_API void lua_settop(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_settop(lua_State *L, int idx) {
|
||||
StkId func = L->ci->func;
|
||||
lua_lock(L);
|
||||
if (idx >= 0) {
|
||||
|
@ -185,8 +176,7 @@ LUA_API void lua_settop(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_remove(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_remove(lua_State *L, int idx) {
|
||||
StkId p;
|
||||
lua_lock(L);
|
||||
p = index2addr(L, idx);
|
||||
|
@ -197,8 +187,7 @@ LUA_API void lua_remove(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_insert(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_insert(lua_State *L, int idx) {
|
||||
StkId p;
|
||||
StkId q;
|
||||
lua_lock(L);
|
||||
|
@ -211,8 +200,7 @@ LUA_API void lua_insert(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
static void moveto(lua_State *L, TValue *fr, int idx)
|
||||
{
|
||||
static void moveto(lua_State *L, TValue *fr, int idx) {
|
||||
TValue *to = index2addr(L, idx);
|
||||
api_checkvalidindex(L, to);
|
||||
setobj(L, to, fr);
|
||||
|
@ -223,8 +211,7 @@ static void moveto(lua_State *L, TValue *fr, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_replace(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_replace(lua_State *L, int idx) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
moveto(L, L->top - 1, idx);
|
||||
|
@ -233,8 +220,7 @@ LUA_API void lua_replace(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
lua_lock(L);
|
||||
fr = index2addr(L, fromidx);
|
||||
|
@ -243,8 +229,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, index2addr(L, idx));
|
||||
api_incr_top(L);
|
||||
|
@ -258,59 +243,51 @@ 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) {
|
||||
StkId o = index2addr(L, idx);
|
||||
return (isvalid(o) ? ttypenv(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);
|
||||
return ttypename(t);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_iscfunction(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API int lua_iscfunction(lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
return (ttislcf(o) || (ttisCclosure(o)));
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_isnumber(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API int lua_isnumber(lua_State *L, int idx) {
|
||||
TValue n;
|
||||
const TValue *o = index2addr(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) {
|
||||
int t = lua_type(L, idx);
|
||||
return (t == LUA_TSTRING || t == LUA_TNUMBER);
|
||||
}
|
||||
|
||||
|
||||
LUA_API int lua_isuserdata(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API int lua_isuserdata(lua_State *L, int idx) {
|
||||
const TValue *o = index2addr(L, idx);
|
||||
return (ttisuserdata(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) {
|
||||
StkId o1 = index2addr(L, index1);
|
||||
StkId o2 = index2addr(L, index2);
|
||||
return (isvalid(o1) && isvalid(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) {
|
||||
StkId o1; /* 1st operand */
|
||||
StkId o2; /* 2nd operand */
|
||||
lua_lock(L);
|
||||
|
@ -332,8 +309,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) {
|
||||
StkId o1, o2;
|
||||
int i = 0;
|
||||
lua_lock(L); /* may call tag method */
|
||||
|
@ -359,8 +335,7 @@ LUA_API int lua_compare(lua_State *L, int index1, int index2, int op)
|
|||
}
|
||||
|
||||
|
||||
LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *isnum)
|
||||
{
|
||||
LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *isnum) {
|
||||
TValue n;
|
||||
const TValue *o = index2addr(L, idx);
|
||||
if (tonumber(o, &n)) {
|
||||
|
@ -373,8 +348,7 @@ 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 lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum) {
|
||||
TValue n;
|
||||
const TValue *o = index2addr(L, idx);
|
||||
if (tonumber(o, &n)) {
|
||||
|
@ -390,8 +364,7 @@ LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum)
|
|||
}
|
||||
|
||||
|
||||
LUA_API lua_Unsigned lua_tounsignedx(lua_State *L, int idx, int *isnum)
|
||||
{
|
||||
LUA_API lua_Unsigned lua_tounsignedx(lua_State *L, int idx, int *isnum) {
|
||||
TValue n;
|
||||
const TValue *o = index2addr(L, idx);
|
||||
if (tonumber(o, &n)) {
|
||||
|
@ -407,15 +380,13 @@ LUA_API lua_Unsigned lua_tounsignedx(lua_State *L, int idx, int *isnum)
|
|||
}
|
||||
|
||||
|
||||
LUA_API int lua_toboolean(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API int lua_toboolean(lua_State *L, int idx) {
|
||||
const TValue *o = index2addr(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) {
|
||||
StkId o = index2addr(L, idx);
|
||||
if (!ttisstring(o)) {
|
||||
lua_lock(L); /* `luaV_tostring' may create a new string */
|
||||
|
@ -433,8 +404,7 @@ LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len)
|
|||
}
|
||||
|
||||
|
||||
LUA_API size_t lua_rawlen(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API size_t lua_rawlen(lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttypenv(o)) {
|
||||
case LUA_TSTRING:
|
||||
|
@ -449,8 +419,7 @@ LUA_API size_t lua_rawlen(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
if (ttislcf(o)) return fvalue(o);
|
||||
else if (ttisCclosure(o))
|
||||
|
@ -459,8 +428,7 @@ LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void *lua_touserdata(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void *lua_touserdata(lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttypenv(o)) {
|
||||
case LUA_TUSERDATA:
|
||||
|
@ -473,15 +441,13 @@ LUA_API void *lua_touserdata(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API lua_State *lua_tothread(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API lua_State *lua_tothread(lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
return (!ttisthread(o)) ? NULL : thvalue(o);
|
||||
}
|
||||
|
||||
|
||||
LUA_API const void *lua_topointer(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API const void *lua_topointer(lua_State *L, int idx) {
|
||||
StkId o = index2addr(L, idx);
|
||||
switch (ttype(o)) {
|
||||
case LUA_TTABLE:
|
||||
|
@ -509,8 +475,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(L->top);
|
||||
api_incr_top(L);
|
||||
|
@ -518,8 +483,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);
|
||||
setnvalue(L->top, n);
|
||||
luai_checknum(L, L->top,
|
||||
|
@ -529,8 +493,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);
|
||||
setnvalue(L->top, cast_num(n));
|
||||
api_incr_top(L);
|
||||
|
@ -538,8 +501,7 @@ LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_pushunsigned(lua_State *L, lua_Unsigned u)
|
||||
{
|
||||
LUA_API void lua_pushunsigned(lua_State *L, lua_Unsigned u) {
|
||||
lua_Number n;
|
||||
lua_lock(L);
|
||||
n = lua_unsigned2number(u);
|
||||
|
@ -549,8 +511,7 @@ LUA_API void lua_pushunsigned(lua_State *L, lua_Unsigned u)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
luaC_checkGC(L);
|
||||
|
@ -562,8 +523,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) {
|
||||
if (s == NULL) {
|
||||
lua_pushnil(L);
|
||||
return NULL;
|
||||
|
@ -581,8 +541,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,
|
||||
va_list argp)
|
||||
{
|
||||
va_list argp) {
|
||||
const char *ret;
|
||||
lua_lock(L);
|
||||
luaC_checkGC(L);
|
||||
|
@ -592,8 +551,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);
|
||||
|
@ -606,8 +564,7 @@ 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(L->top, fn);
|
||||
|
@ -628,8 +585,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);
|
||||
setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
|
||||
api_incr_top(L);
|
||||
|
@ -637,8 +593,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(L->top, p);
|
||||
api_incr_top(L);
|
||||
|
@ -646,8 +601,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, L->top, L);
|
||||
api_incr_top(L);
|
||||
|
@ -662,8 +616,7 @@ LUA_API int lua_pushthread(lua_State *L)
|
|||
*/
|
||||
|
||||
|
||||
LUA_API void lua_getglobal(lua_State *L, const char *var)
|
||||
{
|
||||
LUA_API void lua_getglobal(lua_State *L, const char *var) {
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt; /* global table */
|
||||
lua_lock(L);
|
||||
|
@ -674,8 +627,7 @@ LUA_API void lua_getglobal(lua_State *L, const char *var)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_gettable(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_gettable(lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
|
@ -684,8 +636,7 @@ LUA_API void lua_gettable(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
|
||||
{
|
||||
LUA_API void lua_getfield(lua_State *L, int idx, const char *k) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
|
@ -696,8 +647,7 @@ LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawget(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_rawget(lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
|
@ -707,8 +657,7 @@ LUA_API void lua_rawget(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
|
||||
{
|
||||
LUA_API void lua_rawgeti(lua_State *L, int idx, int n) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
|
@ -719,8 +668,7 @@ LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawgetp(lua_State *L, int idx, const void *p)
|
||||
{
|
||||
LUA_API void lua_rawgetp(lua_State *L, int idx, const void *p) {
|
||||
StkId t;
|
||||
TValue k;
|
||||
lua_lock(L);
|
||||
|
@ -733,8 +681,7 @@ LUA_API void 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);
|
||||
luaC_checkGC(L);
|
||||
|
@ -747,8 +694,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 = NULL;
|
||||
int res;
|
||||
|
@ -777,8 +723,7 @@ LUA_API int lua_getmetatable(lua_State *L, int objindex)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_getuservalue(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_getuservalue(lua_State *L, int idx) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
o = index2addr(L, idx);
|
||||
|
@ -797,8 +742,7 @@ LUA_API void lua_getuservalue(lua_State *L, int idx)
|
|||
*/
|
||||
|
||||
|
||||
LUA_API void lua_setglobal(lua_State *L, const char *var)
|
||||
{
|
||||
LUA_API void lua_setglobal(lua_State *L, const char *var) {
|
||||
Table *reg = hvalue(&G(L)->l_registry);
|
||||
const TValue *gt; /* global table */
|
||||
lua_lock(L);
|
||||
|
@ -811,8 +755,7 @@ LUA_API void lua_setglobal(lua_State *L, const char *var)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_settable(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_settable(lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
|
@ -823,8 +766,7 @@ LUA_API void lua_settable(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
|
||||
{
|
||||
LUA_API void lua_setfield(lua_State *L, int idx, const char *k) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
|
@ -836,8 +778,7 @@ LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawset(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_rawset(lua_State *L, int idx) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
|
@ -851,8 +792,7 @@ LUA_API void lua_rawset(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
|
||||
{
|
||||
LUA_API void lua_rawseti(lua_State *L, int idx, int n) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
|
@ -865,8 +805,7 @@ LUA_API void lua_rawseti(lua_State *L, int idx, int n)
|
|||
}
|
||||
|
||||
|
||||
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) {
|
||||
StkId t;
|
||||
TValue k;
|
||||
lua_lock(L);
|
||||
|
@ -881,8 +820,7 @@ 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_setmetatable(lua_State *L, int objindex) {
|
||||
TValue *obj;
|
||||
Table *mt;
|
||||
lua_lock(L);
|
||||
|
@ -922,8 +860,7 @@ LUA_API int lua_setmetatable(lua_State *L, int objindex)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_setuservalue(lua_State *L, int idx)
|
||||
{
|
||||
LUA_API void lua_setuservalue(lua_State *L, int idx) {
|
||||
StkId o;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
|
@ -951,8 +888,7 @@ LUA_API void lua_setuservalue(lua_State *L, int idx)
|
|||
"results from function overflow current stack size")
|
||||
|
||||
|
||||
LUA_API int lua_getctx(lua_State *L, int *ctx)
|
||||
{
|
||||
LUA_API int lua_getctx(lua_State *L, int *ctx) {
|
||||
if (L->ci->callstatus & CIST_YIELDED) {
|
||||
if (ctx) *ctx = L->ci->u.c.ctx;
|
||||
return L->ci->u.c.status;
|
||||
|
@ -961,8 +897,7 @@ LUA_API int lua_getctx(lua_State *L, int *ctx)
|
|||
|
||||
|
||||
LUA_API void lua_callk(lua_State *L, int nargs, int nresults, int ctx,
|
||||
lua_CFunction k)
|
||||
{
|
||||
lua_CFunction k) {
|
||||
StkId func;
|
||||
lua_lock(L);
|
||||
api_check(L, k == NULL || !isLua(L->ci),
|
||||
|
@ -992,8 +927,7 @@ 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_call(L, c->func, c->nresults, 0);
|
||||
}
|
||||
|
@ -1001,8 +935,7 @@ static void f_call(lua_State *L, void *ud)
|
|||
|
||||
|
||||
LUA_API int lua_pcallk(lua_State *L, int nargs, int nresults, int errfunc,
|
||||
int ctx, lua_CFunction k)
|
||||
{
|
||||
int ctx, lua_CFunction k) {
|
||||
struct CallS c;
|
||||
int status;
|
||||
ptrdiff_t func;
|
||||
|
@ -1046,8 +979,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,
|
||||
const char *chunkname, const char *mode)
|
||||
{
|
||||
const char *chunkname, const char *mode) {
|
||||
ZIO z;
|
||||
int status;
|
||||
lua_lock(L);
|
||||
|
@ -1070,8 +1002,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)
|
||||
{
|
||||
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) {
|
||||
int status;
|
||||
TValue *o;
|
||||
lua_lock(L);
|
||||
|
@ -1086,8 +1017,7 @@ LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
|
|||
}
|
||||
|
||||
|
||||
LUA_API int lua_status(lua_State *L)
|
||||
{
|
||||
LUA_API int lua_status(lua_State *L) {
|
||||
return L->status;
|
||||
}
|
||||
|
||||
|
@ -1096,8 +1026,7 @@ LUA_API int lua_status(lua_State *L)
|
|||
** Garbage-collection function
|
||||
*/
|
||||
|
||||
LUA_API int lua_gc(lua_State *L, int what, int data)
|
||||
{
|
||||
LUA_API int lua_gc(lua_State *L, int what, int data) {
|
||||
int res = 0;
|
||||
global_State *g;
|
||||
lua_lock(L);
|
||||
|
@ -1181,8 +1110,7 @@ LUA_API int lua_gc(lua_State *L, int what, int data)
|
|||
*/
|
||||
|
||||
|
||||
LUA_API int lua_error(lua_State *L)
|
||||
{
|
||||
LUA_API int lua_error(lua_State *L) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
luaG_errormsg(L);
|
||||
|
@ -1191,8 +1119,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) {
|
||||
StkId t;
|
||||
int more;
|
||||
lua_lock(L);
|
||||
|
@ -1208,8 +1135,7 @@ LUA_API int lua_next(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void lua_concat(lua_State *L, int n)
|
||||
{
|
||||
LUA_API void lua_concat(lua_State *L, int n) {
|
||||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
if (n >= 2) {
|
||||
|
@ -1224,8 +1150,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) {
|
||||
StkId t;
|
||||
lua_lock(L);
|
||||
t = index2addr(L, idx);
|
||||
|
@ -1235,8 +1160,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;
|
||||
|
@ -1246,8 +1170,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;
|
||||
|
@ -1255,8 +1178,7 @@ LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void *lua_newuserdata(lua_State *L, size_t size)
|
||||
{
|
||||
LUA_API void *lua_newuserdata(lua_State *L, size_t size) {
|
||||
Udata *u;
|
||||
lua_lock(L);
|
||||
luaC_checkGC(L);
|
||||
|
@ -1270,8 +1192,7 @@ LUA_API void *lua_newuserdata(lua_State *L, size_t size)
|
|||
|
||||
|
||||
static const char *aux_upvalue(StkId fi, int n, TValue **val,
|
||||
GCObject **owner)
|
||||
{
|
||||
GCObject **owner) {
|
||||
switch (ttype(fi)) {
|
||||
case LUA_TCCL: { /* C closure */
|
||||
CClosure *f = clCvalue(fi);
|
||||
|
@ -1296,8 +1217,7 @@ static const char *aux_upvalue(StkId fi, int n, TValue **val,
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -1311,8 +1231,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 */
|
||||
|
@ -1331,8 +1250,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) {
|
||||
LClosure *f;
|
||||
StkId fi = index2addr(L, fidx);
|
||||
api_check(L, ttisLclosure(fi), "Lua function expected");
|
||||
|
@ -1343,8 +1261,7 @@ static UpVal **getupvalref(lua_State *L, int fidx, int n, LClosure **pf)
|
|||
}
|
||||
|
||||
|
||||
LUA_API void *lua_upvalueid(lua_State *L, int fidx, int n)
|
||||
{
|
||||
LUA_API void *lua_upvalueid(lua_State *L, int fidx, int n) {
|
||||
StkId fi = index2addr(L, fidx);
|
||||
switch (ttype(fi)) {
|
||||
case LUA_TLCL: { /* lua closure */
|
||||
|
@ -1364,8 +1281,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,
|
||||
int fidx2, int n2)
|
||||
{
|
||||
int fidx2, int n2) {
|
||||
LClosure *f1;
|
||||
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
|
||||
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
|
||||
|
|
188
liblua/lauxlib.c
188
liblua/lauxlib.c
|
@ -40,8 +40,7 @@
|
|||
** search for 'objidx' in table at index -1.
|
||||
** return 1 + string at top if find 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 */
|
||||
|
@ -64,8 +63,7 @@ static int findfield(lua_State *L, int objidx, int level)
|
|||
}
|
||||
|
||||
|
||||
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_pushglobaltable(L);
|
||||
|
@ -80,8 +78,7 @@ static int pushglobalfuncname(lua_State *L, lua_Debug *ar)
|
|||
}
|
||||
|
||||
|
||||
static void pushfuncname(lua_State *L, lua_Debug *ar)
|
||||
{
|
||||
static void pushfuncname(lua_State *L, lua_Debug *ar) {
|
||||
if (*ar->namewhat != '\0') /* is there a name? */
|
||||
lua_pushfstring(L, "function " LUA_QS, ar->name);
|
||||
else if (*ar->what == 'm') /* main? */
|
||||
|
@ -97,8 +94,7 @@ static void pushfuncname(lua_State *L, lua_Debug *ar)
|
|||
}
|
||||
|
||||
|
||||
static int countlevels(lua_State *L)
|
||||
{
|
||||
static int countlevels(lua_State *L) {
|
||||
lua_Debug ar;
|
||||
int li = 1, le = 1;
|
||||
/* find an upper bound */
|
||||
|
@ -114,8 +110,7 @@ static int countlevels(lua_State *L)
|
|||
|
||||
|
||||
LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1,
|
||||
const char *msg, int level)
|
||||
{
|
||||
const char *msg, int level) {
|
||||
lua_Debug ar;
|
||||
int top = lua_gettop(L);
|
||||
int numlevels = countlevels(L1);
|
||||
|
@ -150,8 +145,7 @@ LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1,
|
|||
** =======================================================
|
||||
*/
|
||||
|
||||
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
|
||||
{
|
||||
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
|
||||
return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
|
||||
|
@ -169,22 +163,19 @@ LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
|
|||
}
|
||||
|
||||
|
||||
static int typeerror(lua_State *L, int narg, const char *tname)
|
||||
{
|
||||
static int typeerror(lua_State *L, int narg, const char *tname) {
|
||||
const char *msg = lua_pushfstring(L, "%s expected, got %s",
|
||||
tname, luaL_typename(L, narg));
|
||||
return luaL_argerror(L, narg, msg);
|
||||
}
|
||||
|
||||
|
||||
static void tag_error(lua_State *L, int narg, int tag)
|
||||
{
|
||||
static void tag_error(lua_State *L, int narg, int tag) {
|
||||
typeerror(L, narg, lua_typename(L, tag));
|
||||
}
|
||||
|
||||
|
||||
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 */
|
||||
|
@ -197,8 +188,7 @@ LUALIB_API void luaL_where(lua_State *L, int level)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -209,8 +199,7 @@ 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);
|
||||
|
@ -249,8 +238,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) {
|
||||
const char *what = "exit"; /* type of termination */
|
||||
if (stat == -1) /* error? */
|
||||
return luaL_fileresult(L, 0, NULL);
|
||||
|
@ -275,8 +263,7 @@ LUALIB_API int luaL_execresult(lua_State *L, int stat)
|
|||
** =======================================================
|
||||
*/
|
||||
|
||||
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
|
||||
{
|
||||
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname) {
|
||||
luaL_getmetatable(L, tname); /* try to get metatable */
|
||||
if (!lua_isnil(L, -1)) /* name already in use? */
|
||||
return 0; /* leave previous value on top, but return 0 */
|
||||
|
@ -288,15 +275,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? */
|
||||
|
@ -311,8 +296,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);
|
||||
if (p == NULL) typeerror(L, ud, tname);
|
||||
return p;
|
||||
|
@ -328,8 +312,7 @@ LUALIB_API void *luaL_checkudata(lua_State *L, int ud, const char *tname)
|
|||
*/
|
||||
|
||||
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def,
|
||||
const char *const lst[])
|
||||
{
|
||||
const char *const lst[]) {
|
||||
const char *name = (def) ? luaL_optstring(L, narg, def) :
|
||||
luaL_checkstring(L, narg);
|
||||
int i;
|
||||
|
@ -341,8 +324,7 @@ LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def,
|
|||
}
|
||||
|
||||
|
||||
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) {
|
||||
/* keep some extra space to run error routines, if needed */
|
||||
const int extra = LUA_MINSTACK;
|
||||
if (!lua_checkstack(L, space + extra)) {
|
||||
|
@ -354,22 +336,19 @@ LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *msg)
|
|||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
|
||||
{
|
||||
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
|
||||
if (lua_type(L, narg) != t)
|
||||
tag_error(L, narg, t);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkany(lua_State *L, int narg)
|
||||
{
|
||||
LUALIB_API void luaL_checkany(lua_State *L, int narg) {
|
||||
if (lua_type(L, narg) == LUA_TNONE)
|
||||
luaL_argerror(L, narg, "value expected");
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_checklstring(lua_State *L, int narg, size_t *len)
|
||||
{
|
||||
LUALIB_API const char *luaL_checklstring(lua_State *L, int narg, size_t *len) {
|
||||
const char *s = lua_tolstring(L, narg, len);
|
||||
if (!s) tag_error(L, narg, LUA_TSTRING);
|
||||
return s;
|
||||
|
@ -377,8 +356,7 @@ LUALIB_API const char *luaL_checklstring(lua_State *L, int narg, size_t *len)
|
|||
|
||||
|
||||
LUALIB_API const char *luaL_optlstring(lua_State *L, int narg,
|
||||
const char *def, size_t *len)
|
||||
{
|
||||
const char *def, size_t *len) {
|
||||
if (lua_isnoneornil(L, narg)) {
|
||||
if (len)
|
||||
*len = (def ? strlen(def) : 0);
|
||||
|
@ -387,8 +365,7 @@ LUALIB_API const char *luaL_optlstring(lua_State *L, int narg,
|
|||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
|
||||
{
|
||||
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg) {
|
||||
int isnum;
|
||||
lua_Number d = lua_tonumberx(L, narg, &isnum);
|
||||
if (!isnum)
|
||||
|
@ -397,14 +374,12 @@ LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
|
|||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)
|
||||
{
|
||||
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def) {
|
||||
return luaL_opt(L, luaL_checknumber, narg, def);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
|
||||
{
|
||||
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg) {
|
||||
int isnum;
|
||||
lua_Integer d = lua_tointegerx(L, narg, &isnum);
|
||||
if (!isnum)
|
||||
|
@ -413,8 +388,7 @@ LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
|
|||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Unsigned luaL_checkunsigned(lua_State *L, int narg)
|
||||
{
|
||||
LUALIB_API lua_Unsigned luaL_checkunsigned(lua_State *L, int narg) {
|
||||
int isnum;
|
||||
lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);
|
||||
if (!isnum)
|
||||
|
@ -424,15 +398,13 @@ LUALIB_API lua_Unsigned luaL_checkunsigned(lua_State *L, int narg)
|
|||
|
||||
|
||||
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg,
|
||||
lua_Integer def)
|
||||
{
|
||||
lua_Integer def) {
|
||||
return luaL_opt(L, luaL_checkinteger, narg, def);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_Unsigned luaL_optunsigned(lua_State *L, int narg,
|
||||
lua_Unsigned def)
|
||||
{
|
||||
lua_Unsigned def) {
|
||||
return luaL_opt(L, luaL_checkunsigned, narg, def);
|
||||
}
|
||||
|
||||
|
@ -455,8 +427,7 @@ LUALIB_API lua_Unsigned luaL_optunsigned(lua_State *L, int narg,
|
|||
/*
|
||||
** 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) {
|
||||
lua_State *L = B->L;
|
||||
if (B->size - B->n < sz) { /* not enough space? */
|
||||
char *newbuff;
|
||||
|
@ -478,22 +449,19 @@ 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_addlstring(luaL_Buffer *B, const char *s, size_t l) {
|
||||
char *b = luaL_prepbuffsize(B, l);
|
||||
memcpy(b, s, l * sizeof(char));
|
||||
luaL_addsize(B, 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;
|
||||
lua_pushlstring(L, B->b, B->n);
|
||||
if (buffonstack(B))
|
||||
|
@ -501,15 +469,13 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
|
||||
{
|
||||
LUALIB_API void luaL_addvalue(luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
size_t l;
|
||||
const char *s = lua_tolstring(L, -1, &l);
|
||||
|
@ -520,8 +486,7 @@ 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->initb;
|
||||
B->n = 0;
|
||||
|
@ -529,8 +494,7 @@ LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
|
|||
}
|
||||
|
||||
|
||||
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 luaL_prepbuffsize(B, sz);
|
||||
}
|
||||
|
@ -548,8 +512,7 @@ LUALIB_API char *luaL_buffinitsize(lua_State *L, luaL_Buffer *B, size_t sz)
|
|||
#define freelist 0
|
||||
|
||||
|
||||
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 */
|
||||
|
@ -569,8 +532,7 @@ LUALIB_API int luaL_ref(lua_State *L, int t)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -596,8 +558,7 @@ 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? */
|
||||
|
@ -614,8 +575,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) {
|
||||
const char *serr = strerror(errno);
|
||||
const char *filename = lua_tostring(L, fnameindex) + 1;
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
|
||||
|
@ -624,8 +584,7 @@ static int errfile(lua_State *L, const char *what, int fnameindex)
|
|||
}
|
||||
|
||||
|
||||
static int skipBOM(LoadF *lf)
|
||||
{
|
||||
static int skipBOM(LoadF *lf) {
|
||||
const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
|
||||
int c;
|
||||
lf->n = 0;
|
||||
|
@ -646,8 +605,7 @@ static int skipBOM(LoadF *lf)
|
|||
** first "valid" character of the file (after the optional BOM and
|
||||
** a first-line comment).
|
||||
*/
|
||||
static int skipcomment(LoadF *lf, int *cp)
|
||||
{
|
||||
static int skipcomment(LoadF *lf, int *cp) {
|
||||
int c = *cp = skipBOM(lf);
|
||||
if (c == '#') { /* first line is a comment (Unix exec. file)? */
|
||||
do { /* skip first line */
|
||||
|
@ -660,8 +618,7 @@ static int skipcomment(LoadF *lf, int *cp)
|
|||
|
||||
|
||||
LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename,
|
||||
const char *mode)
|
||||
{
|
||||
const char *mode) {
|
||||
LoadF lf;
|
||||
int status, readstatus;
|
||||
int c;
|
||||
|
@ -701,8 +658,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;
|
||||
|
@ -713,8 +669,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,
|
||||
const char *name, const char *mode)
|
||||
{
|
||||
const char *name, const char *mode) {
|
||||
LoadS ls;
|
||||
ls.s = buff;
|
||||
ls.size = size;
|
||||
|
@ -722,8 +677,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);
|
||||
}
|
||||
|
||||
|
@ -731,8 +685,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 0;
|
||||
lua_pushstring(L, event);
|
||||
|
@ -747,8 +700,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)) /* no metafield? */
|
||||
return 0;
|
||||
|
@ -758,8 +710,7 @@ LUALIB_API int luaL_callmeta(lua_State *L, int obj, const char *event)
|
|||
}
|
||||
|
||||
|
||||
LUALIB_API int luaL_len(lua_State *L, int idx)
|
||||
{
|
||||
LUALIB_API int luaL_len(lua_State *L, int idx) {
|
||||
int l;
|
||||
int isnum;
|
||||
lua_len(L, idx);
|
||||
|
@ -771,8 +722,7 @@ LUALIB_API int luaL_len(lua_State *L, int idx)
|
|||
}
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len)
|
||||
{
|
||||
LUALIB_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len) {
|
||||
if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
|
||||
switch (lua_type(L, idx)) {
|
||||
case LUA_TNUMBER:
|
||||
|
@ -803,8 +753,7 @@ LUALIB_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len)
|
|||
#if defined(LUA_COMPAT_MODULE)
|
||||
|
||||
static const char *luaL_findtable(lua_State *L, int idx,
|
||||
const char *fname, int szhint)
|
||||
{
|
||||
const char *fname, int szhint) {
|
||||
const char *e;
|
||||
if (idx) lua_pushvalue(L, idx);
|
||||
do {
|
||||
|
@ -832,8 +781,7 @@ static const char *luaL_findtable(lua_State *L, int idx,
|
|||
/*
|
||||
** Count number of elements in a luaL_Reg list.
|
||||
*/
|
||||
static int libsize(const luaL_Reg *l)
|
||||
{
|
||||
static int libsize(const luaL_Reg *l) {
|
||||
int size = 0;
|
||||
for (; l && l->name; l++) size++;
|
||||
return size;
|
||||
|
@ -847,8 +795,7 @@ static int libsize(const luaL_Reg *l)
|
|||
** the module table.
|
||||
*/
|
||||
LUALIB_API void luaL_pushmodule(lua_State *L, const char *modname,
|
||||
int sizehint)
|
||||
{
|
||||
int sizehint) {
|
||||
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
|
||||
lua_getfield(L, -1, modname); /* get _LOADED[modname] */
|
||||
if (!lua_istable(L, -1)) { /* not found? */
|
||||
|
@ -865,8 +812,7 @@ LUALIB_API void luaL_pushmodule(lua_State *L, const char *modname,
|
|||
|
||||
|
||||
LUALIB_API void luaL_openlib(lua_State *L, const char *libname,
|
||||
const luaL_Reg *l, int nup)
|
||||
{
|
||||
const luaL_Reg *l, int nup) {
|
||||
luaL_checkversion(L);
|
||||
if (libname) {
|
||||
luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
|
||||
|
@ -886,8 +832,7 @@ LUALIB_API void luaL_openlib(lua_State *L, const char *libname,
|
|||
** 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_checkversion(L);
|
||||
luaL_checkstack(L, nup, "too many upvalues");
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
|
@ -905,8 +850,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) {
|
||||
lua_getfield(L, idx, fname);
|
||||
if (lua_istable(L, -1)) return 1; /* table already there */
|
||||
else {
|
||||
|
@ -927,8 +871,7 @@ LUALIB_API int luaL_getsubtable(lua_State *L, int idx, const char *fname)
|
|||
** Leaves resulting module on the top.
|
||||
*/
|
||||
LUALIB_API void luaL_requiref(lua_State *L, const char *modname,
|
||||
lua_CFunction openf, int glb)
|
||||
{
|
||||
lua_CFunction openf, int glb) {
|
||||
lua_pushcfunction(L, openf);
|
||||
lua_pushstring(L, modname); /* argument to open function */
|
||||
lua_call(L, 1, 1); /* open module */
|
||||
|
@ -944,8 +887,7 @@ LUALIB_API void luaL_requiref(lua_State *L, const char *modname,
|
|||
|
||||
|
||||
LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, const char *p,
|
||||
const char *r)
|
||||
{
|
||||
const char *r) {
|
||||
const char *wild;
|
||||
size_t l = strlen(p);
|
||||
luaL_Buffer b;
|
||||
|
@ -961,8 +903,7 @@ LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, const char *p,
|
|||
}
|
||||
|
||||
|
||||
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
|
||||
{
|
||||
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
(void)ud;
|
||||
(void)osize; /* not used */
|
||||
if (nsize == 0) {
|
||||
|
@ -973,24 +914,21 @@ static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
|
|||
}
|
||||
|
||||
|
||||
static int panic(lua_State *L)
|
||||
{
|
||||
static int panic(lua_State *L) {
|
||||
luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
lua_tostring(L, -1));
|
||||
return 0; /* return to Lua to abort */
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_State *luaL_newstate(void)
|
||||
{
|
||||
LUALIB_API lua_State *luaL_newstate(void) {
|
||||
lua_State *L = lua_newstate(l_alloc, NULL);
|
||||
if (L) lua_atpanic(L, &panic);
|
||||
return L;
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API void luaL_checkversion_(lua_State *L, lua_Number ver)
|
||||
{
|
||||
LUALIB_API void luaL_checkversion_(lua_State *L, lua_Number ver) {
|
||||
const lua_Number *v = lua_version(L);
|
||||
if (v != lua_version(NULL))
|
||||
luaL_error(L, "multiple Lua VMs detected");
|
||||
|
@ -1000,7 +938,7 @@ LUALIB_API void luaL_checkversion_(lua_State *L, lua_Number ver)
|
|||
/* check conversions number -> integer types */
|
||||
lua_pushnumber(L, -(lua_Number)0x1234);
|
||||
if (lua_tointeger(L, -1) != -0x1234 ||
|
||||
lua_tounsigned(L, -1) != (lua_Unsigned) - 0x1234)
|
||||
lua_tounsigned(L, -1) != (lua_Unsigned) - 0x1234)
|
||||
luaL_error(L, "bad conversion number->int;"
|
||||
" must recompile Lua with proper settings");
|
||||
lua_pop(L, 1);
|
||||
|
|
|
@ -20,8 +20,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;
|
||||
lua_getglobal(L, "tostring");
|
||||
|
@ -46,8 +45,7 @@ static int luaB_print(lua_State *L)
|
|||
|
||||
#define SPACECHARS " \f\n\r\t\v"
|
||||
|
||||
static int luaB_tonumber(lua_State *L)
|
||||
{
|
||||
static int luaB_tonumber(lua_State *L) {
|
||||
if (lua_isnoneornil(L, 2)) { /* standard conversion */
|
||||
int isnum;
|
||||
lua_Number n = lua_tonumberx(L, 1, &isnum);
|
||||
|
@ -87,8 +85,7 @@ static int luaB_tonumber(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_error(lua_State *L)
|
||||
{
|
||||
static int luaB_error(lua_State *L) {
|
||||
int level = luaL_optint(L, 2, 1);
|
||||
lua_settop(L, 1);
|
||||
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
|
||||
|
@ -100,8 +97,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);
|
||||
|
@ -112,8 +108,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_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||
|
@ -126,8 +121,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));
|
||||
|
@ -135,8 +129,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_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
|
||||
"table or string expected");
|
||||
|
@ -145,8 +138,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);
|
||||
|
@ -154,8 +146,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);
|
||||
|
@ -165,8 +156,7 @@ static int luaB_rawset(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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",
|
||||
"setmajorinc", "isrunning", "generational", "incremental", NULL
|
||||
|
@ -198,8 +188,7 @@ static int luaB_collectgarbage(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_type(lua_State *L)
|
||||
{
|
||||
static int luaB_type(lua_State *L) {
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushstring(L, luaL_typename(L, 1));
|
||||
return 1;
|
||||
|
@ -207,8 +196,7 @@ static int luaB_type(lua_State *L)
|
|||
|
||||
|
||||
static int pairsmeta(lua_State *L, const char *method, int iszero,
|
||||
lua_CFunction iter)
|
||||
{
|
||||
lua_CFunction iter) {
|
||||
if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
|
||||
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
|
||||
lua_pushcfunction(L, iter); /* will return generator, */
|
||||
|
@ -223,8 +211,7 @@ static int pairsmeta(lua_State *L, const char *method, int iszero,
|
|||
}
|
||||
|
||||
|
||||
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))
|
||||
|
@ -236,14 +223,12 @@ static int luaB_next(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_pairs(lua_State *L)
|
||||
{
|
||||
static int luaB_pairs(lua_State *L) {
|
||||
return pairsmeta(L, "__pairs", 0, luaB_next);
|
||||
}
|
||||
|
||||
|
||||
static int ipairsaux(lua_State *L)
|
||||
{
|
||||
static int ipairsaux(lua_State *L) {
|
||||
int i = luaL_checkint(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
i++; /* next value */
|
||||
|
@ -253,14 +238,12 @@ static int ipairsaux(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_ipairs(lua_State *L)
|
||||
{
|
||||
static int luaB_ipairs(lua_State *L) {
|
||||
return pairsmeta(L, "__ipairs", 1, ipairsaux);
|
||||
}
|
||||
|
||||
|
||||
static int load_aux(lua_State *L, int status, int envidx)
|
||||
{
|
||||
static int load_aux(lua_State *L, int status, int envidx) {
|
||||
if (status == LUA_OK) {
|
||||
if (envidx != 0) { /* 'env' parameter? */
|
||||
lua_pushvalue(L, envidx); /* environment for loaded function */
|
||||
|
@ -276,8 +259,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' */
|
||||
|
@ -307,8 +289,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 */
|
||||
|
@ -324,8 +305,7 @@ static const char *generic_reader(lua_State *L, void *ud, size_t *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);
|
||||
|
@ -346,14 +326,12 @@ static int luaB_load(lua_State *L)
|
|||
/* }====================================================== */
|
||||
|
||||
|
||||
static int dofilecont(lua_State *L)
|
||||
{
|
||||
static int dofilecont(lua_State *L) {
|
||||
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 (luaL_loadfile(L, fname) != LUA_OK)
|
||||
|
@ -363,16 +341,14 @@ static int luaB_dofile(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_assert(lua_State *L)
|
||||
{
|
||||
static int luaB_assert(lua_State *L) {
|
||||
if (!lua_toboolean(L, 1))
|
||||
return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
|
||||
return lua_gettop(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);
|
||||
|
@ -387,8 +363,7 @@ static int luaB_select(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int finishpcall(lua_State *L, int status)
|
||||
{
|
||||
static int finishpcall(lua_State *L, int status) {
|
||||
if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */
|
||||
lua_settop(L, 0); /* create space for return values */
|
||||
lua_pushboolean(L, 0);
|
||||
|
@ -401,15 +376,13 @@ static int finishpcall(lua_State *L, int status)
|
|||
}
|
||||
|
||||
|
||||
static int pcallcont(lua_State *L)
|
||||
{
|
||||
static int pcallcont(lua_State *L) {
|
||||
int status = lua_getctx(L, NULL);
|
||||
return finishpcall(L, (status == LUA_YIELD));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_pcall(lua_State *L)
|
||||
{
|
||||
static int luaB_pcall(lua_State *L) {
|
||||
int status;
|
||||
luaL_checkany(L, 1);
|
||||
lua_pushnil(L);
|
||||
|
@ -419,8 +392,7 @@ static int luaB_pcall(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_xpcall(lua_State *L)
|
||||
{
|
||||
static int luaB_xpcall(lua_State *L) {
|
||||
int status;
|
||||
int n = lua_gettop(L);
|
||||
luaL_argcheck(L, n >= 2, 2, "value expected");
|
||||
|
@ -432,8 +404,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;
|
||||
|
@ -470,8 +441,7 @@ static const luaL_Reg base_funcs[] = {
|
|||
};
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_base(lua_State *L)
|
||||
{
|
||||
LUAMOD_API int luaopen_base(lua_State *L) {
|
||||
/* set global _G */
|
||||
lua_pushglobaltable(L);
|
||||
lua_pushglobaltable(L);
|
||||
|
|
|
@ -33,8 +33,7 @@ typedef lua_Unsigned b_uint;
|
|||
|
||||
|
||||
|
||||
static b_uint andaux(lua_State *L)
|
||||
{
|
||||
static b_uint andaux(lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = ~(b_uint)0;
|
||||
for (i = 1; i <= n; i++)
|
||||
|
@ -43,24 +42,21 @@ static b_uint andaux(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int b_and(lua_State *L)
|
||||
{
|
||||
static int b_and(lua_State *L) {
|
||||
b_uint r = andaux(L);
|
||||
lua_pushunsigned(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_test(lua_State *L)
|
||||
{
|
||||
static int b_test(lua_State *L) {
|
||||
b_uint r = andaux(L);
|
||||
lua_pushboolean(L, r != 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_or(lua_State *L)
|
||||
{
|
||||
static int b_or(lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
|
@ -70,8 +66,7 @@ static int b_or(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int b_xor(lua_State *L)
|
||||
{
|
||||
static int b_xor(lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
|
@ -81,16 +76,14 @@ static int b_xor(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int b_not(lua_State *L)
|
||||
{
|
||||
static int b_not(lua_State *L) {
|
||||
b_uint r = ~luaL_checkunsigned(L, 1);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_shift(lua_State *L, b_uint r, int i)
|
||||
{
|
||||
static int b_shift(lua_State *L, b_uint r, int i) {
|
||||
if (i < 0) { /* shift right? */
|
||||
i = -i;
|
||||
r = trim(r);
|
||||
|
@ -106,20 +99,17 @@ static int b_shift(lua_State *L, b_uint r, int i)
|
|||
}
|
||||
|
||||
|
||||
static int b_lshift(lua_State *L)
|
||||
{
|
||||
static int b_lshift(lua_State *L) {
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rshift(lua_State *L)
|
||||
{
|
||||
static int b_rshift(lua_State *L) {
|
||||
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_arshift(lua_State *L)
|
||||
{
|
||||
static int b_arshift(lua_State *L) {
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
int i = luaL_checkint(L, 2);
|
||||
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
|
||||
|
@ -134,8 +124,7 @@ static int b_arshift(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int b_rot(lua_State *L, int i)
|
||||
{
|
||||
static int b_rot(lua_State *L, int i) {
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
i &= (LUA_NBITS - 1); /* i = i % NBITS */
|
||||
r = trim(r);
|
||||
|
@ -145,14 +134,12 @@ static int b_rot(lua_State *L, int i)
|
|||
}
|
||||
|
||||
|
||||
static int b_lrot(lua_State *L)
|
||||
{
|
||||
static int b_lrot(lua_State *L) {
|
||||
return b_rot(L, luaL_checkint(L, 2));
|
||||
}
|
||||
|
||||
|
||||
static int b_rrot(lua_State *L)
|
||||
{
|
||||
static int b_rrot(lua_State *L) {
|
||||
return b_rot(L, -luaL_checkint(L, 2));
|
||||
}
|
||||
|
||||
|
@ -163,8 +150,7 @@ static int b_rrot(lua_State *L)
|
|||
** ('luaL_error' called without 'return' to avoid later warnings about
|
||||
** 'width' being used uninitialized.)
|
||||
*/
|
||||
static int fieldargs(lua_State *L, int farg, int *width)
|
||||
{
|
||||
static int fieldargs(lua_State *L, int farg, int *width) {
|
||||
int f = luaL_checkint(L, farg);
|
||||
int w = luaL_optint(L, farg + 1, 1);
|
||||
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
||||
|
@ -176,8 +162,7 @@ static int fieldargs(lua_State *L, int farg, int *width)
|
|||
}
|
||||
|
||||
|
||||
static int b_extract(lua_State *L)
|
||||
{
|
||||
static int b_extract(lua_State *L) {
|
||||
int w;
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
int f = fieldargs(L, 2, &w);
|
||||
|
@ -187,8 +172,7 @@ static int b_extract(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int b_replace(lua_State *L)
|
||||
{
|
||||
static int b_replace(lua_State *L) {
|
||||
int w;
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
b_uint v = luaL_checkunsigned(L, 2);
|
||||
|
@ -219,8 +203,7 @@ static const luaL_Reg bitlib[] = {
|
|||
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_bit32(lua_State *L)
|
||||
{
|
||||
LUAMOD_API int luaopen_bit32(lua_State *L) {
|
||||
luaL_newlib(L, bitlib);
|
||||
return 1;
|
||||
}
|
||||
|
|
182
liblua/lcode.c
182
liblua/lcode.c
|
@ -29,14 +29,12 @@
|
|||
#define hasjumps(e) ((e)->t != (e)->f)
|
||||
|
||||
|
||||
static int isnumeral(expdesc *e)
|
||||
{
|
||||
static int isnumeral(expdesc *e) {
|
||||
return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
|
||||
}
|
||||
|
||||
|
||||
void luaK_nil(FuncState *fs, int from, int n)
|
||||
{
|
||||
void luaK_nil(FuncState *fs, int from, int n) {
|
||||
Instruction *previous;
|
||||
int l = from + n - 1; /* last register to set nil */
|
||||
if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
|
||||
|
@ -45,7 +43,7 @@ void luaK_nil(FuncState *fs, int from, int n)
|
|||
int pfrom = GETARG_A(*previous);
|
||||
int pl = pfrom + GETARG_B(*previous);
|
||||
if ((pfrom <= from && from <= pl + 1) ||
|
||||
(from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
|
||||
(from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
|
||||
if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
|
||||
if (pl > l) l = pl; /* l = max(l, pl) */
|
||||
SETARG_A(*previous, from);
|
||||
|
@ -58,8 +56,7 @@ void luaK_nil(FuncState *fs, int from, int n)
|
|||
}
|
||||
|
||||
|
||||
int luaK_jump(FuncState *fs)
|
||||
{
|
||||
int luaK_jump(FuncState *fs) {
|
||||
int jpc = fs->jpc; /* save list of jumps to here */
|
||||
int j;
|
||||
fs->jpc = NO_JUMP;
|
||||
|
@ -69,21 +66,18 @@ int luaK_jump(FuncState *fs)
|
|||
}
|
||||
|
||||
|
||||
void luaK_ret(FuncState *fs, int first, int nret)
|
||||
{
|
||||
void luaK_ret(FuncState *fs, int first, int nret) {
|
||||
luaK_codeABC(fs, OP_RETURN, first, nret + 1, 0);
|
||||
}
|
||||
|
||||
|
||||
static int condjump(FuncState *fs, OpCode op, int A, int B, int C)
|
||||
{
|
||||
static int condjump(FuncState *fs, OpCode op, int A, int B, int C) {
|
||||
luaK_codeABC(fs, op, A, B, C);
|
||||
return luaK_jump(fs);
|
||||
}
|
||||
|
||||
|
||||
static void fixjump(FuncState *fs, int pc, int dest)
|
||||
{
|
||||
static void fixjump(FuncState *fs, int pc, int dest) {
|
||||
Instruction *jmp = &fs->f->code[pc];
|
||||
int offset = dest - (pc + 1);
|
||||
lua_assert(dest != NO_JUMP);
|
||||
|
@ -97,15 +91,13 @@ static void fixjump(FuncState *fs, int pc, int dest)
|
|||
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||
** optimizations with consecutive instructions not in the same basic block).
|
||||
*/
|
||||
int luaK_getlabel(FuncState *fs)
|
||||
{
|
||||
int luaK_getlabel(FuncState *fs) {
|
||||
fs->lasttarget = fs->pc;
|
||||
return fs->pc;
|
||||
}
|
||||
|
||||
|
||||
static int getjump(FuncState *fs, int pc)
|
||||
{
|
||||
static int getjump(FuncState *fs, int pc) {
|
||||
int offset = GETARG_sBx(fs->f->code[pc]);
|
||||
if (offset == NO_JUMP) /* point to itself represents end of list */
|
||||
return NO_JUMP; /* end of list */
|
||||
|
@ -114,8 +106,7 @@ static int getjump(FuncState *fs, int pc)
|
|||
}
|
||||
|
||||
|
||||
static Instruction *getjumpcontrol(FuncState *fs, int pc)
|
||||
{
|
||||
static Instruction *getjumpcontrol(FuncState *fs, int pc) {
|
||||
Instruction *pi = &fs->f->code[pc];
|
||||
if (pc >= 1 && testTMode(GET_OPCODE(*(pi - 1))))
|
||||
return pi - 1;
|
||||
|
@ -128,8 +119,7 @@ static Instruction *getjumpcontrol(FuncState *fs, int pc)
|
|||
** check whether list has any jump that do not produce a value
|
||||
** (or produce an inverted value)
|
||||
*/
|
||||
static int need_value(FuncState *fs, int list)
|
||||
{
|
||||
static int need_value(FuncState *fs, int list) {
|
||||
for (; list != NO_JUMP; list = getjump(fs, list)) {
|
||||
Instruction i = *getjumpcontrol(fs, list);
|
||||
if (GET_OPCODE(i) != OP_TESTSET) return 1;
|
||||
|
@ -138,8 +128,7 @@ static int need_value(FuncState *fs, int list)
|
|||
}
|
||||
|
||||
|
||||
static int patchtestreg(FuncState *fs, int node, int reg)
|
||||
{
|
||||
static int patchtestreg(FuncState *fs, int node, int reg) {
|
||||
Instruction *i = getjumpcontrol(fs, node);
|
||||
if (GET_OPCODE(*i) != OP_TESTSET)
|
||||
return 0; /* cannot patch other instructions */
|
||||
|
@ -152,16 +141,14 @@ static int patchtestreg(FuncState *fs, int node, int reg)
|
|||
}
|
||||
|
||||
|
||||
static void removevalues(FuncState *fs, int list)
|
||||
{
|
||||
static void removevalues(FuncState *fs, int list) {
|
||||
for (; list != NO_JUMP; list = getjump(fs, list))
|
||||
patchtestreg(fs, list, NO_REG);
|
||||
}
|
||||
|
||||
|
||||
static void patchlistaux(FuncState *fs, int list, int vtarget, int reg,
|
||||
int dtarget)
|
||||
{
|
||||
int dtarget) {
|
||||
while (list != NO_JUMP) {
|
||||
int next = getjump(fs, list);
|
||||
if (patchtestreg(fs, list, reg))
|
||||
|
@ -173,15 +160,13 @@ static void patchlistaux(FuncState *fs, int list, int vtarget, int reg,
|
|||
}
|
||||
|
||||
|
||||
static void dischargejpc(FuncState *fs)
|
||||
{
|
||||
static void dischargejpc(FuncState *fs) {
|
||||
patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
|
||||
fs->jpc = NO_JUMP;
|
||||
}
|
||||
|
||||
|
||||
void luaK_patchlist(FuncState *fs, int list, int target)
|
||||
{
|
||||
void luaK_patchlist(FuncState *fs, int list, int target) {
|
||||
if (target == fs->pc)
|
||||
luaK_patchtohere(fs, list);
|
||||
else {
|
||||
|
@ -191,8 +176,7 @@ void luaK_patchlist(FuncState *fs, int list, int target)
|
|||
}
|
||||
|
||||
|
||||
LUAI_FUNC void luaK_patchclose(FuncState *fs, int list, int level)
|
||||
{
|
||||
LUAI_FUNC void luaK_patchclose(FuncState *fs, int list, int level) {
|
||||
level++; /* argument is +1 to reserve 0 as non-op */
|
||||
while (list != NO_JUMP) {
|
||||
int next = getjump(fs, list);
|
||||
|
@ -205,15 +189,13 @@ LUAI_FUNC void luaK_patchclose(FuncState *fs, int list, int level)
|
|||
}
|
||||
|
||||
|
||||
void luaK_patchtohere(FuncState *fs, int list)
|
||||
{
|
||||
void luaK_patchtohere(FuncState *fs, int list) {
|
||||
luaK_getlabel(fs);
|
||||
luaK_concat(fs, &fs->jpc, list);
|
||||
}
|
||||
|
||||
|
||||
void luaK_concat(FuncState *fs, int *l1, int l2)
|
||||
{
|
||||
void luaK_concat(FuncState *fs, int *l1, int l2) {
|
||||
if (l2 == NO_JUMP) return;
|
||||
else if (*l1 == NO_JUMP)
|
||||
*l1 = l2;
|
||||
|
@ -227,8 +209,7 @@ void luaK_concat(FuncState *fs, int *l1, int l2)
|
|||
}
|
||||
|
||||
|
||||
static int luaK_code(FuncState *fs, Instruction i)
|
||||
{
|
||||
static int luaK_code(FuncState *fs, Instruction i) {
|
||||
Proto *f = fs->f;
|
||||
dischargejpc(fs); /* `pc' will change */
|
||||
/* put new instruction in code array */
|
||||
|
@ -243,8 +224,7 @@ static int luaK_code(FuncState *fs, Instruction i)
|
|||
}
|
||||
|
||||
|
||||
int luaK_codeABC(FuncState *fs, OpCode o, int a, int b, int c)
|
||||
{
|
||||
int luaK_codeABC(FuncState *fs, OpCode o, int a, int b, int c) {
|
||||
lua_assert(getOpMode(o) == iABC);
|
||||
lua_assert(getBMode(o) != OpArgN || b == 0);
|
||||
lua_assert(getCMode(o) != OpArgN || c == 0);
|
||||
|
@ -253,8 +233,7 @@ int luaK_codeABC(FuncState *fs, OpCode o, int a, int b, int c)
|
|||
}
|
||||
|
||||
|
||||
int luaK_codeABx(FuncState *fs, OpCode o, int a, unsigned int bc)
|
||||
{
|
||||
int luaK_codeABx(FuncState *fs, OpCode o, int a, unsigned int bc) {
|
||||
lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
|
||||
lua_assert(getCMode(o) == OpArgN);
|
||||
lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
|
||||
|
@ -262,15 +241,13 @@ int luaK_codeABx(FuncState *fs, OpCode o, int a, unsigned int bc)
|
|||
}
|
||||
|
||||
|
||||
static int codeextraarg(FuncState *fs, int a)
|
||||
{
|
||||
static int codeextraarg(FuncState *fs, int a) {
|
||||
lua_assert(a <= MAXARG_Ax);
|
||||
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
|
||||
}
|
||||
|
||||
|
||||
int luaK_codek(FuncState *fs, int reg, int k)
|
||||
{
|
||||
int luaK_codek(FuncState *fs, int reg, int k) {
|
||||
if (k <= MAXARG_Bx)
|
||||
return luaK_codeABx(fs, OP_LOADK, reg, k);
|
||||
else {
|
||||
|
@ -281,8 +258,7 @@ int luaK_codek(FuncState *fs, int reg, int k)
|
|||
}
|
||||
|
||||
|
||||
void luaK_checkstack(FuncState *fs, int n)
|
||||
{
|
||||
void luaK_checkstack(FuncState *fs, int n) {
|
||||
int newstack = fs->freereg + n;
|
||||
if (newstack > fs->f->maxstacksize) {
|
||||
if (newstack >= MAXSTACK)
|
||||
|
@ -292,15 +268,13 @@ void luaK_checkstack(FuncState *fs, int n)
|
|||
}
|
||||
|
||||
|
||||
void luaK_reserveregs(FuncState *fs, int n)
|
||||
{
|
||||
void luaK_reserveregs(FuncState *fs, int n) {
|
||||
luaK_checkstack(fs, n);
|
||||
fs->freereg += n;
|
||||
}
|
||||
|
||||
|
||||
static void freereg(FuncState *fs, int reg)
|
||||
{
|
||||
static void freereg(FuncState *fs, int reg) {
|
||||
if (!ISK(reg) && reg >= fs->nactvar) {
|
||||
fs->freereg--;
|
||||
lua_assert(reg == fs->freereg);
|
||||
|
@ -308,15 +282,13 @@ static void freereg(FuncState *fs, int reg)
|
|||
}
|
||||
|
||||
|
||||
static void freeexp(FuncState *fs, expdesc *e)
|
||||
{
|
||||
static void freeexp(FuncState *fs, expdesc *e) {
|
||||
if (e->k == VNONRELOC)
|
||||
freereg(fs, e->u.info);
|
||||
}
|
||||
|
||||
|
||||
static int addk(FuncState *fs, TValue *key, TValue *v)
|
||||
{
|
||||
static int addk(FuncState *fs, TValue *key, TValue *v) {
|
||||
lua_State *L = fs->ls->L;
|
||||
TValue *idx = luaH_set(L, fs->h, key);
|
||||
Proto *f = fs->f;
|
||||
|
@ -344,16 +316,14 @@ static int addk(FuncState *fs, TValue *key, TValue *v)
|
|||
}
|
||||
|
||||
|
||||
int luaK_stringK(FuncState *fs, TString *s)
|
||||
{
|
||||
int luaK_stringK(FuncState *fs, TString *s) {
|
||||
TValue o;
|
||||
setsvalue(fs->ls->L, &o, s);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
int luaK_numberK(FuncState *fs, lua_Number r)
|
||||
{
|
||||
int luaK_numberK(FuncState *fs, lua_Number r) {
|
||||
int n;
|
||||
lua_State *L = fs->ls->L;
|
||||
TValue o;
|
||||
|
@ -369,16 +339,14 @@ int luaK_numberK(FuncState *fs, lua_Number r)
|
|||
}
|
||||
|
||||
|
||||
static int boolK(FuncState *fs, int b)
|
||||
{
|
||||
static int boolK(FuncState *fs, int b) {
|
||||
TValue o;
|
||||
setbvalue(&o, b);
|
||||
return addk(fs, &o, &o);
|
||||
}
|
||||
|
||||
|
||||
static int nilK(FuncState *fs)
|
||||
{
|
||||
static int nilK(FuncState *fs) {
|
||||
TValue k, v;
|
||||
setnilvalue(&v);
|
||||
/* cannot use nil as key; instead use table itself to represent nil */
|
||||
|
@ -387,8 +355,7 @@ static int nilK(FuncState *fs)
|
|||
}
|
||||
|
||||
|
||||
void luaK_setreturns(FuncState *fs, expdesc *e, int nresults)
|
||||
{
|
||||
void luaK_setreturns(FuncState *fs, expdesc *e, int nresults) {
|
||||
if (e->k == VCALL) { /* expression is an open function call? */
|
||||
SETARG_C(getcode(fs, e), nresults + 1);
|
||||
} else if (e->k == VVARARG) {
|
||||
|
@ -399,8 +366,7 @@ void luaK_setreturns(FuncState *fs, expdesc *e, int nresults)
|
|||
}
|
||||
|
||||
|
||||
void luaK_setoneret(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_setoneret(FuncState *fs, expdesc *e) {
|
||||
if (e->k == VCALL) { /* expression is an open function call? */
|
||||
e->k = VNONRELOC;
|
||||
e->u.info = GETARG_A(getcode(fs, e));
|
||||
|
@ -411,8 +377,7 @@ void luaK_setoneret(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
void luaK_dischargevars(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_dischargevars(FuncState *fs, expdesc *e) {
|
||||
switch (e->k) {
|
||||
case VLOCAL: {
|
||||
e->k = VNONRELOC;
|
||||
|
@ -445,15 +410,13 @@ void luaK_dischargevars(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
static int code_label(FuncState *fs, int A, int b, int jump)
|
||||
{
|
||||
static int code_label(FuncState *fs, int A, int b, int jump) {
|
||||
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
||||
}
|
||||
|
||||
|
||||
static void discharge2reg(FuncState *fs, expdesc *e, int reg)
|
||||
{
|
||||
static void discharge2reg(FuncState *fs, expdesc *e, int reg) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL: {
|
||||
|
@ -493,8 +456,7 @@ static void discharge2reg(FuncState *fs, expdesc *e, int reg)
|
|||
}
|
||||
|
||||
|
||||
static void discharge2anyreg(FuncState *fs, expdesc *e)
|
||||
{
|
||||
static void discharge2anyreg(FuncState *fs, expdesc *e) {
|
||||
if (e->k != VNONRELOC) {
|
||||
luaK_reserveregs(fs, 1);
|
||||
discharge2reg(fs, e, fs->freereg - 1);
|
||||
|
@ -502,8 +464,7 @@ static void discharge2anyreg(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
static void exp2reg(FuncState *fs, expdesc *e, int reg)
|
||||
{
|
||||
static void exp2reg(FuncState *fs, expdesc *e, int reg) {
|
||||
discharge2reg(fs, e, reg);
|
||||
if (e->k == VJMP)
|
||||
luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
|
||||
|
@ -527,8 +488,7 @@ static void exp2reg(FuncState *fs, expdesc *e, int reg)
|
|||
}
|
||||
|
||||
|
||||
void luaK_exp2nextreg(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_exp2nextreg(FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
freeexp(fs, e);
|
||||
luaK_reserveregs(fs, 1);
|
||||
|
@ -536,8 +496,7 @@ void luaK_exp2nextreg(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
int luaK_exp2anyreg(FuncState *fs, expdesc *e)
|
||||
{
|
||||
int luaK_exp2anyreg(FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
if (e->k == VNONRELOC) {
|
||||
if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
|
||||
|
@ -551,15 +510,13 @@ int luaK_exp2anyreg(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
void luaK_exp2anyregup(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_exp2anyregup(FuncState *fs, expdesc *e) {
|
||||
if (e->k != VUPVAL || hasjumps(e))
|
||||
luaK_exp2anyreg(fs, e);
|
||||
}
|
||||
|
||||
|
||||
void luaK_exp2val(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_exp2val(FuncState *fs, expdesc *e) {
|
||||
if (hasjumps(e))
|
||||
luaK_exp2anyreg(fs, e);
|
||||
else
|
||||
|
@ -567,8 +524,7 @@ void luaK_exp2val(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
int luaK_exp2RK(FuncState *fs, expdesc *e)
|
||||
{
|
||||
int luaK_exp2RK(FuncState *fs, expdesc *e) {
|
||||
luaK_exp2val(fs, e);
|
||||
switch (e->k) {
|
||||
case VTRUE:
|
||||
|
@ -598,8 +554,7 @@ int luaK_exp2RK(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
void luaK_storevar(FuncState *fs, expdesc *var, expdesc *ex)
|
||||
{
|
||||
void luaK_storevar(FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
switch (var->k) {
|
||||
case VLOCAL: {
|
||||
freeexp(fs, ex);
|
||||
|
@ -626,8 +581,7 @@ void luaK_storevar(FuncState *fs, expdesc *var, expdesc *ex)
|
|||
}
|
||||
|
||||
|
||||
void luaK_self(FuncState *fs, expdesc *e, expdesc *key)
|
||||
{
|
||||
void luaK_self(FuncState *fs, expdesc *e, expdesc *key) {
|
||||
int ereg;
|
||||
luaK_exp2anyreg(fs, e);
|
||||
ereg = e->u.info; /* register where 'e' was placed */
|
||||
|
@ -640,8 +594,7 @@ void luaK_self(FuncState *fs, expdesc *e, expdesc *key)
|
|||
}
|
||||
|
||||
|
||||
static void invertjump(FuncState *fs, expdesc *e)
|
||||
{
|
||||
static void invertjump(FuncState *fs, expdesc *e) {
|
||||
Instruction *pc = getjumpcontrol(fs, e->u.info);
|
||||
lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
|
||||
GET_OPCODE(*pc) != OP_TEST);
|
||||
|
@ -649,8 +602,7 @@ static void invertjump(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
static int jumponcond(FuncState *fs, expdesc *e, int cond)
|
||||
{
|
||||
static int jumponcond(FuncState *fs, expdesc *e, int cond) {
|
||||
if (e->k == VRELOCABLE) {
|
||||
Instruction ie = getcode(fs, e);
|
||||
if (GET_OPCODE(ie) == OP_NOT) {
|
||||
|
@ -665,8 +617,7 @@ static int jumponcond(FuncState *fs, expdesc *e, int cond)
|
|||
}
|
||||
|
||||
|
||||
void luaK_goiftrue(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_goiftrue(FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
|
@ -692,8 +643,7 @@ void luaK_goiftrue(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
void luaK_goiffalse(FuncState *fs, expdesc *e)
|
||||
{
|
||||
void luaK_goiffalse(FuncState *fs, expdesc *e) {
|
||||
int pc; /* pc of last jump */
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
|
@ -717,8 +667,7 @@ void luaK_goiffalse(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
static void codenot(FuncState *fs, expdesc *e)
|
||||
{
|
||||
static void codenot(FuncState *fs, expdesc *e) {
|
||||
luaK_dischargevars(fs, e);
|
||||
switch (e->k) {
|
||||
case VNIL:
|
||||
|
@ -756,8 +705,7 @@ static void codenot(FuncState *fs, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k)
|
||||
{
|
||||
void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k) {
|
||||
lua_assert(!hasjumps(t));
|
||||
t->u.ind.t = t->u.info;
|
||||
t->u.ind.idx = luaK_exp2RK(fs, k);
|
||||
|
@ -767,8 +715,7 @@ void luaK_indexed(FuncState *fs, expdesc *t, expdesc *k)
|
|||
}
|
||||
|
||||
|
||||
static int constfolding(OpCode op, expdesc *e1, expdesc *e2)
|
||||
{
|
||||
static int constfolding(OpCode op, expdesc *e1, expdesc *e2) {
|
||||
lua_Number r;
|
||||
if (!isnumeral(e1) || !isnumeral(e2)) return 0;
|
||||
if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
|
||||
|
@ -780,8 +727,7 @@ static int constfolding(OpCode op, expdesc *e1, expdesc *e2)
|
|||
|
||||
|
||||
static void codearith(FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int line)
|
||||
{
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
if (constfolding(op, e1, e2))
|
||||
return;
|
||||
else {
|
||||
|
@ -802,8 +748,7 @@ static void codearith(FuncState *fs, OpCode op,
|
|||
|
||||
|
||||
static void codecomp(FuncState *fs, OpCode op, int cond, expdesc *e1,
|
||||
expdesc *e2)
|
||||
{
|
||||
expdesc *e2) {
|
||||
int o1 = luaK_exp2RK(fs, e1);
|
||||
int o2 = luaK_exp2RK(fs, e2);
|
||||
freeexp(fs, e2);
|
||||
|
@ -820,8 +765,7 @@ static void codecomp(FuncState *fs, OpCode op, int cond, expdesc *e1,
|
|||
}
|
||||
|
||||
|
||||
void luaK_prefix(FuncState *fs, UnOpr op, expdesc *e, int line)
|
||||
{
|
||||
void luaK_prefix(FuncState *fs, UnOpr op, expdesc *e, int line) {
|
||||
expdesc e2;
|
||||
e2.t = e2.f = NO_JUMP;
|
||||
e2.k = VKNUM;
|
||||
|
@ -850,8 +794,7 @@ void luaK_prefix(FuncState *fs, UnOpr op, expdesc *e, int line)
|
|||
}
|
||||
|
||||
|
||||
void luaK_infix(FuncState *fs, BinOpr op, expdesc *v)
|
||||
{
|
||||
void luaK_infix(FuncState *fs, BinOpr op, expdesc *v) {
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
luaK_goiftrue(fs, v);
|
||||
|
@ -883,8 +826,7 @@ void luaK_infix(FuncState *fs, BinOpr op, expdesc *v)
|
|||
|
||||
|
||||
void luaK_posfix(FuncState *fs, BinOpr op,
|
||||
expdesc *e1, expdesc *e2, int line)
|
||||
{
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
switch (op) {
|
||||
case OPR_AND: {
|
||||
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
||||
|
@ -941,14 +883,12 @@ void luaK_posfix(FuncState *fs, BinOpr op,
|
|||
}
|
||||
|
||||
|
||||
void luaK_fixline(FuncState *fs, int line)
|
||||
{
|
||||
void luaK_fixline(FuncState *fs, int line) {
|
||||
fs->f->lineinfo[fs->pc - 1] = line;
|
||||
}
|
||||
|
||||
|
||||
void luaK_setlist(FuncState *fs, int base, int nelems, int tostore)
|
||||
{
|
||||
void luaK_setlist(FuncState *fs, int base, int nelems, int tostore) {
|
||||
int c = (nelems - 1) / LFIELDS_PER_FLUSH + 1;
|
||||
int b = (tostore == LUA_MULTRET) ? 0 : tostore;
|
||||
lua_assert(tostore != 0);
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
#include "lualib.h"
|
||||
|
||||
|
||||
static int auxresume(lua_State *L, lua_State *co, int narg)
|
||||
{
|
||||
static int auxresume(lua_State *L, lua_State *co, int narg) {
|
||||
int status;
|
||||
if (!lua_checkstack(co, narg)) {
|
||||
lua_pushliteral(L, "too many arguments to resume");
|
||||
|
@ -46,8 +45,7 @@ static int auxresume(lua_State *L, lua_State *co, int narg)
|
|||
}
|
||||
|
||||
|
||||
static int luaB_coresume(lua_State *L)
|
||||
{
|
||||
static int luaB_coresume(lua_State *L) {
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
int r;
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
|
@ -64,8 +62,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 (r < 0) {
|
||||
|
@ -80,8 +77,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);
|
||||
|
@ -91,22 +87,19 @@ 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));
|
||||
}
|
||||
|
||||
|
||||
static int luaB_costatus(lua_State *L)
|
||||
{
|
||||
static int luaB_costatus(lua_State *L) {
|
||||
lua_State *co = lua_tothread(L, 1);
|
||||
luaL_argcheck(L, co, 1, "coroutine expected");
|
||||
if (L == co) lua_pushliteral(L, "running");
|
||||
|
@ -134,8 +127,7 @@ static int luaB_costatus(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -154,8 +146,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;
|
||||
}
|
||||
|
|
|
@ -22,15 +22,13 @@
|
|||
|
||||
|
||||
|
||||
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 */
|
||||
|
@ -39,8 +37,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_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||
"nil or table expected");
|
||||
|
@ -50,8 +47,7 @@ static int db_setmetatable(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int db_getuservalue(lua_State *L)
|
||||
{
|
||||
static int db_getuservalue(lua_State *L) {
|
||||
if (lua_type(L, 1) != LUA_TUSERDATA)
|
||||
lua_pushnil(L);
|
||||
else
|
||||
|
@ -60,8 +56,7 @@ static int db_getuservalue(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int db_setuservalue(lua_State *L)
|
||||
{
|
||||
static int db_setuservalue(lua_State *L) {
|
||||
if (lua_type(L, 1) == LUA_TLIGHTUSERDATA)
|
||||
luaL_argerror(L, 1, "full userdata expected, got light userdata");
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
|
@ -73,29 +68,25 @@ static int db_setuservalue(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void settabss(lua_State *L, const char *i, const char *v)
|
||||
{
|
||||
static void settabss(lua_State *L, const char *i, const char *v) {
|
||||
lua_pushstring(L, v);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
static void settabsi(lua_State *L, const char *i, int v)
|
||||
{
|
||||
static void settabsi(lua_State *L, const char *i, int v) {
|
||||
lua_pushinteger(L, v);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
static void settabsb(lua_State *L, const char *i, int v)
|
||||
{
|
||||
static void settabsb(lua_State *L, const char *i, int v) {
|
||||
lua_pushboolean(L, v);
|
||||
lua_setfield(L, -2, i);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -106,8 +97,7 @@ static lua_State *getthread(lua_State *L, int *arg)
|
|||
}
|
||||
|
||||
|
||||
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_pushvalue(L, -2);
|
||||
lua_remove(L, -3);
|
||||
|
@ -117,8 +107,7 @@ static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -166,8 +155,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);
|
||||
lua_Debug ar;
|
||||
|
@ -194,8 +182,7 @@ static int db_getlocal(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int db_setlocal(lua_State *L)
|
||||
{
|
||||
static int db_setlocal(lua_State *L) {
|
||||
int arg;
|
||||
lua_State *L1 = getthread(L, &arg);
|
||||
lua_Debug ar;
|
||||
|
@ -209,8 +196,7 @@ static int db_setlocal(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int auxupvalue(lua_State *L, int get)
|
||||
{
|
||||
static int auxupvalue(lua_State *L, int get) {
|
||||
const char *name;
|
||||
int n = luaL_checkint(L, 2);
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
|
@ -222,21 +208,18 @@ static int auxupvalue(lua_State *L, int get)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
static int checkupval(lua_State *L, int argf, int argnup)
|
||||
{
|
||||
static int checkupval(lua_State *L, int argf, int argnup) {
|
||||
lua_Debug ar;
|
||||
int nup = luaL_checkint(L, argnup);
|
||||
luaL_checktype(L, argf, LUA_TFUNCTION);
|
||||
|
@ -247,16 +230,14 @@ static int checkupval(lua_State *L, int argf, int argnup)
|
|||
}
|
||||
|
||||
|
||||
static int db_upvalueid(lua_State *L)
|
||||
{
|
||||
static int db_upvalueid(lua_State *L) {
|
||||
int n = checkupval(L, 1, 2);
|
||||
lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int db_upvaluejoin(lua_State *L)
|
||||
{
|
||||
static int db_upvaluejoin(lua_State *L) {
|
||||
int n1 = checkupval(L, 1, 2);
|
||||
int n2 = checkupval(L, 3, 4);
|
||||
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
|
||||
|
@ -269,8 +250,7 @@ static int db_upvaluejoin(lua_State *L)
|
|||
#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)
|
||||
|
||||
|
||||
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"};
|
||||
gethooktable(L);
|
||||
|
@ -287,8 +267,7 @@ static void hookf(lua_State *L, lua_Debug *ar)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -298,8 +277,7 @@ static int makemask(const char *smask, int count)
|
|||
}
|
||||
|
||||
|
||||
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';
|
||||
|
@ -309,8 +287,7 @@ 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);
|
||||
|
@ -341,8 +318,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];
|
||||
|
@ -363,24 +339,22 @@ 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];
|
||||
luai_writestringerror("%s", "lua_debug> ");
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||
strcmp(buffer, "cont\n") == 0)
|
||||
strcmp(buffer, "cont\n") == 0)
|
||||
return 0;
|
||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||
lua_pcall(L, 0, 0, 0))
|
||||
lua_pcall(L, 0, 0, 0))
|
||||
luai_writestringerror("%s\n", lua_tostring(L, -1));
|
||||
lua_settop(L, 0); /* remove eventual returns */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -415,8 +389,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;
|
||||
}
|
||||
|
|
|
@ -36,15 +36,13 @@
|
|||
static const char *getfuncname(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);
|
||||
}
|
||||
|
||||
|
||||
static int currentline(CallInfo *ci)
|
||||
{
|
||||
static int currentline(CallInfo *ci) {
|
||||
return getfuncline(ci_func(ci)->p, currentpc(ci));
|
||||
}
|
||||
|
||||
|
@ -52,8 +50,7 @@ static int currentline(CallInfo *ci)
|
|||
/*
|
||||
** this function can be called asynchronous (e.g. during a signal)
|
||||
*/
|
||||
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)
|
||||
{
|
||||
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count) {
|
||||
if (func == NULL || mask == 0) { /* turn off hooks? */
|
||||
mask = 0;
|
||||
func = NULL;
|
||||
|
@ -68,26 +65,22 @@ LUA_API int 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 */
|
||||
|
@ -103,16 +96,14 @@ LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)
|
|||
}
|
||||
|
||||
|
||||
static const char *upvalname(Proto *p, int uv)
|
||||
{
|
||||
static const char *upvalname(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) {
|
||||
int nparams = clLvalue(ci->func)->p->numparams;
|
||||
if (n >= ci->u.l.base - ci->func - nparams)
|
||||
return NULL; /* no such vararg */
|
||||
|
@ -124,8 +115,7 @@ static const char *findvararg(CallInfo *ci, int n, StkId *pos)
|
|||
|
||||
|
||||
static const char *findlocal(lua_State *L, CallInfo *ci, int n,
|
||||
StkId *pos)
|
||||
{
|
||||
StkId *pos) {
|
||||
const char *name = NULL;
|
||||
StkId base;
|
||||
if (isLua(ci)) {
|
||||
|
@ -149,8 +139,7 @@ static const char *findlocal(lua_State *L, CallInfo *ci, int n,
|
|||
}
|
||||
|
||||
|
||||
LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
|
||||
{
|
||||
LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) {
|
||||
const char *name;
|
||||
lua_lock(L);
|
||||
if (ar == NULL) { /* information about non-active function? */
|
||||
|
@ -171,8 +160,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 = 0; /* to avoid warnings */
|
||||
const char *name = findlocal(L, ar->i_ci, n, &pos);
|
||||
lua_lock(L);
|
||||
|
@ -184,8 +172,7 @@ 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 (noLuaClosure(cl)) {
|
||||
ar->source = "=[C]";
|
||||
ar->linedefined = -1;
|
||||
|
@ -202,8 +189,7 @@ static void funcinfo(lua_Debug *ar, Closure *cl)
|
|||
}
|
||||
|
||||
|
||||
static void collectvalidlines(lua_State *L, Closure *f)
|
||||
{
|
||||
static void collectvalidlines(lua_State *L, Closure *f) {
|
||||
if (noLuaClosure(f)) {
|
||||
setnilvalue(L->top);
|
||||
api_incr_top(L);
|
||||
|
@ -222,8 +208,7 @@ static void collectvalidlines(lua_State *L, Closure *f)
|
|||
|
||||
|
||||
static int auxgetinfo(lua_State *L, const char *what, lua_Debug *ar,
|
||||
Closure *f, CallInfo *ci)
|
||||
{
|
||||
Closure *f, CallInfo *ci) {
|
||||
int status = 1;
|
||||
for (; *what; what++) {
|
||||
switch (*what) {
|
||||
|
@ -273,8 +258,7 @@ static int auxgetinfo(lua_State *L, const char *what, lua_Debug *ar,
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -317,8 +301,7 @@ static const char *getobjname(Proto *p, int lastpc, int reg,
|
|||
/*
|
||||
** find a "name" for the RK value 'c'
|
||||
*/
|
||||
static void kname(Proto *p, int pc, int c, const char **name)
|
||||
{
|
||||
static void kname(Proto *p, int pc, int c, const char **name) {
|
||||
if (ISK(c)) { /* is 'c' a constant? */
|
||||
TValue *kvalue = &p->k[INDEXK(c)];
|
||||
if (ttisstring(kvalue)) { /* literal constant? */
|
||||
|
@ -340,8 +323,7 @@ static void kname(Proto *p, int pc, int c, const char **name)
|
|||
/*
|
||||
** try to find last instruction before 'lastpc' that modified register 'reg'
|
||||
*/
|
||||
static int findsetreg(Proto *p, int lastpc, int reg)
|
||||
{
|
||||
static int findsetreg(Proto *p, int lastpc, int reg) {
|
||||
int pc;
|
||||
int setreg = -1; /* keep last instruction that changed 'reg' */
|
||||
for (pc = 0; pc < lastpc; pc++) {
|
||||
|
@ -387,8 +369,7 @@ static int findsetreg(Proto *p, int lastpc, int reg)
|
|||
|
||||
|
||||
static const char *getobjname(Proto *p, int lastpc, int reg,
|
||||
const char **name)
|
||||
{
|
||||
const char **name) {
|
||||
int pc;
|
||||
*name = luaF_getlocalname(p, reg + 1, lastpc);
|
||||
if (*name) /* is a local? */
|
||||
|
@ -442,8 +423,7 @@ static const char *getobjname(Proto *p, int lastpc, int reg,
|
|||
}
|
||||
|
||||
|
||||
static const char *getfuncname(lua_State *L, CallInfo *ci, const char **name)
|
||||
{
|
||||
static const char *getfuncname(lua_State *L, CallInfo *ci, const char **name) {
|
||||
TMS tm;
|
||||
Proto *p = ci_func(ci)->p; /* calling function */
|
||||
int pc = currentpc(ci); /* calling instruction index */
|
||||
|
@ -517,8 +497,7 @@ static const char *getfuncname(lua_State *L, CallInfo *ci, const char **name)
|
|||
** only ANSI way to check whether a pointer points to an array
|
||||
** (used only for error messages, so efficiency is not a big concern)
|
||||
*/
|
||||
static int isinstack(CallInfo *ci, const TValue *o)
|
||||
{
|
||||
static int isinstack(CallInfo *ci, const TValue *o) {
|
||||
StkId p;
|
||||
for (p = ci->u.l.base; p < ci->top; p++)
|
||||
if (o == p) return 1;
|
||||
|
@ -527,8 +506,7 @@ static int isinstack(CallInfo *ci, const TValue *o)
|
|||
|
||||
|
||||
static const char *getupvalname(CallInfo *ci, const TValue *o,
|
||||
const char **name)
|
||||
{
|
||||
const char **name) {
|
||||
LClosure *c = ci_func(ci);
|
||||
int i;
|
||||
for (i = 0; i < c->nupvalues; i++) {
|
||||
|
@ -541,8 +519,7 @@ static const char *getupvalname(CallInfo *ci, const TValue *o,
|
|||
}
|
||||
|
||||
|
||||
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) {
|
||||
CallInfo *ci = L->ci;
|
||||
const char *name = NULL;
|
||||
const char *t = objtypename(o);
|
||||
|
@ -561,16 +538,14 @@ l_noret luaG_typeerror(lua_State *L, const TValue *o, const char *op)
|
|||
}
|
||||
|
||||
|
||||
l_noret luaG_concaterror(lua_State *L, StkId p1, StkId p2)
|
||||
{
|
||||
l_noret luaG_concaterror(lua_State *L, StkId p1, StkId p2) {
|
||||
if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
|
||||
lua_assert(!ttisstring(p1) && !ttisnumber(p2));
|
||||
luaG_typeerror(L, p1, "concatenate");
|
||||
}
|
||||
|
||||
|
||||
l_noret luaG_aritherror(lua_State *L, const TValue *p1, const TValue *p2)
|
||||
{
|
||||
l_noret luaG_aritherror(lua_State *L, const TValue *p1, const TValue *p2) {
|
||||
TValue temp;
|
||||
if (luaV_tonumber(p1, &temp) == NULL)
|
||||
p2 = p1; /* first operand is wrong */
|
||||
|
@ -578,8 +553,7 @@ l_noret luaG_aritherror(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 = objtypename(p1);
|
||||
const char *t2 = objtypename(p2);
|
||||
if (t1 == t2)
|
||||
|
@ -589,8 +563,7 @@ l_noret luaG_ordererror(lua_State *L, const TValue *p1, const TValue *p2)
|
|||
}
|
||||
|
||||
|
||||
static void addinfo(lua_State *L, const char *msg)
|
||||
{
|
||||
static void addinfo(lua_State *L, const char *msg) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (isLua(ci)) { /* is Lua code? */
|
||||
char buff[LUA_IDSIZE]; /* add file:line information */
|
||||
|
@ -607,8 +580,7 @@ static void addinfo(lua_State *L, const char *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);
|
||||
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
|
||||
|
@ -621,8 +593,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, ...) {
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
addinfo(L, luaO_pushvfstring(L, fmt, argp));
|
||||
|
|
85
liblua/ldo.c
85
liblua/ldo.c
|
@ -81,8 +81,7 @@ struct lua_longjmp {
|
|||
};
|
||||
|
||||
|
||||
static void seterrorobj(lua_State *L, int errcode, StkId oldtop)
|
||||
{
|
||||
static void seterrorobj(lua_State *L, int errcode, StkId oldtop) {
|
||||
switch (errcode) {
|
||||
case LUA_ERRMEM: { /* memory error? */
|
||||
setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
|
||||
|
@ -101,8 +100,7 @@ static void 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 */
|
||||
|
@ -122,8 +120,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) {
|
||||
unsigned short oldnCcalls = L->nCcalls;
|
||||
struct lua_longjmp lj;
|
||||
lj.status = LUA_OK;
|
||||
|
@ -140,8 +137,7 @@ int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
|
|||
/* }====================================================== */
|
||||
|
||||
|
||||
static void correctstack(lua_State *L, TValue *oldstack)
|
||||
{
|
||||
static void correctstack(lua_State *L, TValue *oldstack) {
|
||||
CallInfo *ci;
|
||||
GCObject *up;
|
||||
L->top = (L->top - oldstack) + L->stack;
|
||||
|
@ -160,8 +156,7 @@ static void correctstack(lua_State *L, TValue *oldstack)
|
|||
#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
|
||||
|
||||
|
||||
void luaD_reallocstack(lua_State *L, int newsize)
|
||||
{
|
||||
void luaD_reallocstack(lua_State *L, int newsize) {
|
||||
TValue *oldstack = L->stack;
|
||||
int lim = L->stacksize;
|
||||
lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
|
||||
|
@ -175,8 +170,7 @@ void luaD_reallocstack(lua_State *L, int newsize)
|
|||
}
|
||||
|
||||
|
||||
void luaD_growstack(lua_State *L, int n)
|
||||
{
|
||||
void luaD_growstack(lua_State *L, int n) {
|
||||
int size = L->stacksize;
|
||||
if (size > LUAI_MAXSTACK) /* error after extra size? */
|
||||
luaD_throw(L, LUA_ERRERR);
|
||||
|
@ -194,8 +188,7 @@ void luaD_growstack(lua_State *L, int n)
|
|||
}
|
||||
|
||||
|
||||
static int stackinuse(lua_State *L)
|
||||
{
|
||||
static int stackinuse(lua_State *L) {
|
||||
CallInfo *ci;
|
||||
StkId lim = L->top;
|
||||
for (ci = L->ci; ci != NULL; ci = ci->previous) {
|
||||
|
@ -206,21 +199,19 @@ static int stackinuse(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
void luaD_shrinkstack(lua_State *L)
|
||||
{
|
||||
void luaD_shrinkstack(lua_State *L) {
|
||||
int inuse = stackinuse(L);
|
||||
int goodsize = inuse + (inuse / 8) + 2 * EXTRA_STACK;
|
||||
if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
|
||||
if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */
|
||||
goodsize >= L->stacksize) /* would grow instead of shrink? */
|
||||
goodsize >= L->stacksize) /* would grow instead of shrink? */
|
||||
condmovestack(L); /* don't change stack (change only for debugging) */
|
||||
else
|
||||
luaD_reallocstack(L, goodsize); /* shrink it */
|
||||
}
|
||||
|
||||
|
||||
void luaD_hook(lua_State *L, int event, int line)
|
||||
{
|
||||
void luaD_hook(lua_State *L, int event, int line) {
|
||||
lua_Hook hook = L->hook;
|
||||
if (hook && L->allowhook) {
|
||||
CallInfo *ci = L->ci;
|
||||
|
@ -247,12 +238,11 @@ void luaD_hook(lua_State *L, int event, int line)
|
|||
}
|
||||
|
||||
|
||||
static void callhook(lua_State *L, CallInfo *ci)
|
||||
{
|
||||
static void callhook(lua_State *L, CallInfo *ci) {
|
||||
int hook = LUA_HOOKCALL;
|
||||
ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
|
||||
if (isLua(ci->previous) &&
|
||||
GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
|
||||
GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
|
||||
ci->callstatus |= CIST_TAIL;
|
||||
hook = LUA_HOOKTAILCALL;
|
||||
}
|
||||
|
@ -261,8 +251,7 @@ static void callhook(lua_State *L, CallInfo *ci)
|
|||
}
|
||||
|
||||
|
||||
static StkId adjust_varargs(lua_State *L, Proto *p, int actual)
|
||||
{
|
||||
static StkId adjust_varargs(lua_State *L, Proto *p, int actual) {
|
||||
int i;
|
||||
int nfixargs = p->numparams;
|
||||
StkId base, fixed;
|
||||
|
@ -278,8 +267,7 @@ static StkId adjust_varargs(lua_State *L, Proto *p, int actual)
|
|||
}
|
||||
|
||||
|
||||
static StkId tryfuncTM(lua_State *L, StkId func)
|
||||
{
|
||||
static StkId tryfuncTM(lua_State *L, StkId func) {
|
||||
const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
|
||||
StkId p;
|
||||
ptrdiff_t funcr = savestack(L, func);
|
||||
|
@ -301,8 +289,7 @@ static StkId tryfuncTM(lua_State *L, StkId func)
|
|||
/*
|
||||
** returns true if function has been executed (C function)
|
||||
*/
|
||||
int luaD_precall(lua_State *L, StkId func, int nresults)
|
||||
{
|
||||
int luaD_precall(lua_State *L, StkId func, int nresults) {
|
||||
lua_CFunction f;
|
||||
CallInfo *ci;
|
||||
int n; /* number of arguments (Lua) or returns (C) */
|
||||
|
@ -362,8 +349,7 @@ Cfunc:
|
|||
}
|
||||
|
||||
|
||||
int luaD_poscall(lua_State *L, StkId firstResult)
|
||||
{
|
||||
int luaD_poscall(lua_State *L, StkId firstResult) {
|
||||
StkId res;
|
||||
int wanted, i;
|
||||
CallInfo *ci = L->ci;
|
||||
|
@ -394,8 +380,7 @@ int luaD_poscall(lua_State *L, StkId firstResult)
|
|||
** When returns, all the results are on the stack, starting at the original
|
||||
** function position.
|
||||
*/
|
||||
void luaD_call(lua_State *L, StkId func, int nResults, int allowyield)
|
||||
{
|
||||
void luaD_call(lua_State *L, StkId func, int nResults, int allowyield) {
|
||||
if (++L->nCcalls >= LUAI_MAXCCALLS) {
|
||||
if (L->nCcalls == LUAI_MAXCCALLS)
|
||||
luaG_runerror(L, "C stack overflow");
|
||||
|
@ -410,8 +395,7 @@ void luaD_call(lua_State *L, StkId func, int nResults, int allowyield)
|
|||
}
|
||||
|
||||
|
||||
static void finishCcall(lua_State *L)
|
||||
{
|
||||
static void finishCcall(lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
int n;
|
||||
lua_assert(ci->u.c.k != NULL); /* must have a continuation */
|
||||
|
@ -436,8 +420,7 @@ static void finishCcall(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void unroll(lua_State *L, void *ud)
|
||||
{
|
||||
static void unroll(lua_State *L, void *ud) {
|
||||
UNUSED(ud);
|
||||
for (;;) {
|
||||
if (L->ci == &L->base_ci) /* stack is empty? */
|
||||
|
@ -455,8 +438,7 @@ static void unroll(lua_State *L, void *ud)
|
|||
/*
|
||||
** check whether thread has a suspended protected call
|
||||
*/
|
||||
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)
|
||||
|
@ -466,8 +448,7 @@ static CallInfo *findpcall(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int recover(lua_State *L, int status)
|
||||
{
|
||||
static int recover(lua_State *L, int status) {
|
||||
StkId oldtop;
|
||||
CallInfo *ci = findpcall(L);
|
||||
if (ci == NULL) return 0; /* no recovery point */
|
||||
|
@ -491,8 +472,7 @@ static int recover(lua_State *L, int status)
|
|||
** coroutine itself. (Such errors should not be handled by any coroutine
|
||||
** error handler and should not kill the coroutine.)
|
||||
*/
|
||||
static l_noret resume_error(lua_State *L, const char *msg, StkId firstArg)
|
||||
{
|
||||
static l_noret resume_error(lua_State *L, const char *msg, StkId firstArg) {
|
||||
L->top = firstArg; /* remove args from the stack */
|
||||
setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
|
||||
api_incr_top(L);
|
||||
|
@ -503,8 +483,7 @@ static l_noret resume_error(lua_State *L, const char *msg, StkId firstArg)
|
|||
/*
|
||||
** do the work for 'lua_resume' in protected mode
|
||||
*/
|
||||
static void resume(lua_State *L, void *ud)
|
||||
{
|
||||
static void resume(lua_State *L, void *ud) {
|
||||
int nCcalls = L->nCcalls;
|
||||
StkId firstArg = cast(StkId, ud);
|
||||
CallInfo *ci = L->ci;
|
||||
|
@ -542,8 +521,7 @@ static void resume(lua_State *L, void *ud)
|
|||
}
|
||||
|
||||
|
||||
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 status;
|
||||
lua_lock(L);
|
||||
luai_userstateresume(L, nargs);
|
||||
|
@ -574,8 +552,7 @@ LUA_API int lua_resume(lua_State *L, lua_State *from, int nargs)
|
|||
}
|
||||
|
||||
|
||||
LUA_API int lua_yieldk(lua_State *L, int nresults, int ctx, lua_CFunction k)
|
||||
{
|
||||
LUA_API int lua_yieldk(lua_State *L, int nresults, int ctx, lua_CFunction k) {
|
||||
CallInfo *ci = L->ci;
|
||||
luai_userstateyield(L, nresults);
|
||||
lua_lock(L);
|
||||
|
@ -603,8 +580,7 @@ LUA_API int lua_yieldk(lua_State *L, int nresults, int ctx, lua_CFunction k)
|
|||
|
||||
|
||||
int luaD_pcall(lua_State *L, Pfunc func, void *u,
|
||||
ptrdiff_t old_top, ptrdiff_t ef)
|
||||
{
|
||||
ptrdiff_t old_top, ptrdiff_t ef) {
|
||||
int status;
|
||||
CallInfo *old_ci = L->ci;
|
||||
lu_byte old_allowhooks = L->allowhook;
|
||||
|
@ -639,8 +615,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 " LUA_QS ")", x, mode);
|
||||
|
@ -649,8 +624,7 @@ 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) {
|
||||
int i;
|
||||
Closure *cl;
|
||||
struct SParser *p = cast(struct SParser *, ud);
|
||||
|
@ -672,8 +646,7 @@ static void f_parser(lua_State *L, void *ud)
|
|||
|
||||
|
||||
int luaD_protectedparser(lua_State *L, ZIO *z, const char *name,
|
||||
const char *mode)
|
||||
{
|
||||
const char *mode) {
|
||||
struct SParser p;
|
||||
int status;
|
||||
L->nny++; /* cannot yield during parsing */
|
||||
|
|
|
@ -26,8 +26,7 @@ typedef struct {
|
|||
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
|
||||
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
|
||||
|
||||
static void DumpBlock(const void *b, size_t size, DumpState *D)
|
||||
{
|
||||
static void DumpBlock(const void *b, size_t size, DumpState *D) {
|
||||
if (D->status == 0) {
|
||||
lua_unlock(D->L);
|
||||
D->status = (*D->writer)(D->L, b, size, D->data);
|
||||
|
@ -35,30 +34,25 @@ static void DumpBlock(const void *b, size_t size, DumpState *D)
|
|||
}
|
||||
}
|
||||
|
||||
static void DumpChar(int y, DumpState *D)
|
||||
{
|
||||
static void DumpChar(int y, DumpState *D) {
|
||||
char x = (char)y;
|
||||
DumpVar(x, D);
|
||||
}
|
||||
|
||||
static void DumpInt(int x, DumpState *D)
|
||||
{
|
||||
static void DumpInt(int x, DumpState *D) {
|
||||
DumpVar(x, D);
|
||||
}
|
||||
|
||||
static void DumpNumber(lua_Number x, DumpState *D)
|
||||
{
|
||||
static void DumpNumber(lua_Number x, DumpState *D) {
|
||||
DumpVar(x, D);
|
||||
}
|
||||
|
||||
static void DumpVector(const void *b, int n, size_t size, DumpState *D)
|
||||
{
|
||||
static void DumpVector(const void *b, int n, size_t size, DumpState *D) {
|
||||
DumpInt(n, D);
|
||||
DumpMem(b, n, size, D);
|
||||
}
|
||||
|
||||
static void DumpString(const TString *s, DumpState *D)
|
||||
{
|
||||
static void DumpString(const TString *s, DumpState *D) {
|
||||
if (s == NULL) {
|
||||
size_t size = 0;
|
||||
DumpVar(size, D);
|
||||
|
@ -73,8 +67,7 @@ static void DumpString(const TString *s, DumpState *D)
|
|||
|
||||
static void DumpFunction(const Proto *f, DumpState *D);
|
||||
|
||||
static void DumpConstants(const Proto *f, DumpState *D)
|
||||
{
|
||||
static void DumpConstants(const Proto *f, DumpState *D) {
|
||||
int i, n = f->sizek;
|
||||
DumpInt(n, D);
|
||||
for (i = 0; i < n; i++) {
|
||||
|
@ -101,8 +94,7 @@ static void DumpConstants(const Proto *f, DumpState *D)
|
|||
for (i = 0; i < n; i++) DumpFunction(f->p[i], D);
|
||||
}
|
||||
|
||||
static void DumpUpvalues(const Proto *f, DumpState *D)
|
||||
{
|
||||
static void DumpUpvalues(const Proto *f, DumpState *D) {
|
||||
int i, n = f->sizeupvalues;
|
||||
DumpInt(n, D);
|
||||
for (i = 0; i < n; i++) {
|
||||
|
@ -111,8 +103,7 @@ static void DumpUpvalues(const Proto *f, DumpState *D)
|
|||
}
|
||||
}
|
||||
|
||||
static void DumpDebug(const Proto *f, DumpState *D)
|
||||
{
|
||||
static void DumpDebug(const Proto *f, DumpState *D) {
|
||||
int i, n;
|
||||
DumpString((D->strip) ? NULL : f->source, D);
|
||||
n = (D->strip) ? 0 : f->sizelineinfo;
|
||||
|
@ -129,8 +120,7 @@ static void DumpDebug(const Proto *f, DumpState *D)
|
|||
for (i = 0; i < n; i++) DumpString(f->upvalues[i].name, D);
|
||||
}
|
||||
|
||||
static void DumpFunction(const Proto *f, DumpState *D)
|
||||
{
|
||||
static void DumpFunction(const Proto *f, DumpState *D) {
|
||||
DumpInt(f->linedefined, D);
|
||||
DumpInt(f->lastlinedefined, D);
|
||||
DumpChar(f->numparams, D);
|
||||
|
@ -142,8 +132,7 @@ static void DumpFunction(const Proto *f, DumpState *D)
|
|||
DumpDebug(f, D);
|
||||
}
|
||||
|
||||
static void DumpHeader(DumpState *D)
|
||||
{
|
||||
static void DumpHeader(DumpState *D) {
|
||||
lu_byte h[LUAC_HEADERSIZE];
|
||||
luaU_header(h);
|
||||
DumpBlock(h, LUAC_HEADERSIZE, D);
|
||||
|
@ -152,8 +141,7 @@ static void DumpHeader(DumpState *D)
|
|||
/*
|
||||
** dump Lua function as precompiled chunk
|
||||
*/
|
||||
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip)
|
||||
{
|
||||
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip) {
|
||||
DumpState D;
|
||||
D.L = L;
|
||||
D.writer = w;
|
||||
|
|
|
@ -20,16 +20,14 @@
|
|||
|
||||
|
||||
|
||||
Closure *luaF_newCclosure(lua_State *L, int n)
|
||||
{
|
||||
Closure *luaF_newCclosure(lua_State *L, int n) {
|
||||
Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl;
|
||||
c->c.nupvalues = cast_byte(n);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
Closure *luaF_newLclosure(lua_State *L, int n)
|
||||
{
|
||||
Closure *luaF_newLclosure(lua_State *L, int n) {
|
||||
Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl;
|
||||
c->l.p = NULL;
|
||||
c->l.nupvalues = cast_byte(n);
|
||||
|
@ -38,8 +36,7 @@ Closure *luaF_newLclosure(lua_State *L, int n)
|
|||
}
|
||||
|
||||
|
||||
UpVal *luaF_newupval(lua_State *L)
|
||||
{
|
||||
UpVal *luaF_newupval(lua_State *L) {
|
||||
UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
|
||||
uv->v = &uv->u.value;
|
||||
setnilvalue(uv->v);
|
||||
|
@ -47,8 +44,7 @@ UpVal *luaF_newupval(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
UpVal *luaF_findupval(lua_State *L, StkId level)
|
||||
{
|
||||
UpVal *luaF_findupval(lua_State *L, StkId level) {
|
||||
global_State *g = G(L);
|
||||
GCObject **pp = &L->openupval;
|
||||
UpVal *p;
|
||||
|
@ -76,24 +72,21 @@ UpVal *luaF_findupval(lua_State *L, StkId level)
|
|||
}
|
||||
|
||||
|
||||
static void unlinkupval(UpVal *uv)
|
||||
{
|
||||
static void unlinkupval(UpVal *uv) {
|
||||
lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
|
||||
uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
|
||||
uv->u.l.prev->u.l.next = uv->u.l.next;
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeupval(lua_State *L, UpVal *uv)
|
||||
{
|
||||
void luaF_freeupval(lua_State *L, UpVal *uv) {
|
||||
if (uv->v != &uv->u.value) /* is it open? */
|
||||
unlinkupval(uv); /* remove from open list */
|
||||
luaM_free(L, uv); /* free upvalue */
|
||||
}
|
||||
|
||||
|
||||
void luaF_close(lua_State *L, StkId level)
|
||||
{
|
||||
void luaF_close(lua_State *L, StkId level) {
|
||||
UpVal *uv;
|
||||
global_State *g = G(L);
|
||||
while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) {
|
||||
|
@ -114,8 +107,7 @@ void luaF_close(lua_State *L, StkId level)
|
|||
}
|
||||
|
||||
|
||||
Proto *luaF_newproto(lua_State *L)
|
||||
{
|
||||
Proto *luaF_newproto(lua_State *L) {
|
||||
Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p;
|
||||
f->k = NULL;
|
||||
f->sizek = 0;
|
||||
|
@ -140,8 +132,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);
|
||||
|
@ -156,8 +147,7 @@ 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++) {
|
||||
if (pc < f->locvars[i].endpc) { /* is variable active? */
|
||||
|
|
160
liblua/lgc.c
160
liblua/lgc.c
|
@ -104,8 +104,7 @@ static void reallymarkobject(global_State *g, GCObject *o);
|
|||
** if key is not marked, mark its entry as dead (therefore removing it
|
||||
** from the table)
|
||||
*/
|
||||
static void removeentry(Node *n)
|
||||
{
|
||||
static void removeentry(Node *n) {
|
||||
lua_assert(ttisnil(gval(n)));
|
||||
if (valiswhite(gkey(n)))
|
||||
setdeadvalue(gkey(n)); /* unused and unmarked key; remove it */
|
||||
|
@ -119,8 +118,7 @@ static void removeentry(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 TValue *o)
|
||||
{
|
||||
static int iscleared(global_State *g, const TValue *o) {
|
||||
if (!iscollectable(o)) return 0;
|
||||
else if (ttisstring(o)) {
|
||||
markobject(g, rawtsvalue(o)); /* strings are `values', so are never weak */
|
||||
|
@ -133,8 +131,7 @@ static int iscleared(global_State *g, const TValue *o)
|
|||
** barrier that moves collector forward, that is, mark the white object
|
||||
** being pointed by a black object.
|
||||
*/
|
||||
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));
|
||||
lua_assert(g->gcstate != GCSpause);
|
||||
|
@ -154,8 +151,7 @@ void luaC_barrier_(lua_State *L, GCObject *o, GCObject *v)
|
|||
** only works for tables; access to 'gclist' is not uniform across
|
||||
** different types.)
|
||||
*/
|
||||
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) && gch(o)->tt == LUA_TTABLE);
|
||||
black2gray(o); /* make object gray (again) */
|
||||
|
@ -172,8 +168,7 @@ void luaC_barrierback_(lua_State *L, GCObject *o)
|
|||
** it again. Otherwise, use a backward barrier, to avoid marking all
|
||||
** possible instances.
|
||||
*/
|
||||
LUAI_FUNC void luaC_barrierproto_(lua_State *L, Proto *p, Closure *c)
|
||||
{
|
||||
LUAI_FUNC void luaC_barrierproto_(lua_State *L, Proto *p, Closure *c) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(isblack(obj2gco(p)));
|
||||
if (p->cache == NULL) { /* first time? */
|
||||
|
@ -190,8 +185,7 @@ LUAI_FUNC void luaC_barrierproto_(lua_State *L, Proto *p, Closure *c)
|
|||
** check color (and invariants) for an upvalue that was closed,
|
||||
** i.e., moved into the 'allgc' list
|
||||
*/
|
||||
void luaC_checkupvalcolor(global_State *g, UpVal *uv)
|
||||
{
|
||||
void luaC_checkupvalcolor(global_State *g, UpVal *uv) {
|
||||
GCObject *o = obj2gco(uv);
|
||||
lua_assert(!isblack(o)); /* open upvalues are never black */
|
||||
if (isgray(o)) {
|
||||
|
@ -213,8 +207,7 @@ void luaC_checkupvalcolor(global_State *g, UpVal *uv)
|
|||
** object itself (used only by states).
|
||||
*/
|
||||
GCObject *luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list,
|
||||
int offset)
|
||||
{
|
||||
int offset) {
|
||||
global_State *g = G(L);
|
||||
char *raw = cast(char *, luaM_newobject(L, novariant(tt), sz));
|
||||
GCObject *o = obj2gco(raw + offset);
|
||||
|
@ -244,8 +237,7 @@ GCObject *luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list,
|
|||
** to appropriate list to be visited (and turned black) later. (Open
|
||||
** upvalues are already linked in 'headuv' list.)
|
||||
*/
|
||||
static void reallymarkobject(global_State *g, GCObject *o)
|
||||
{
|
||||
static void reallymarkobject(global_State *g, GCObject *o) {
|
||||
lu_mem size;
|
||||
white2gray(o);
|
||||
switch (gch(o)->tt) {
|
||||
|
@ -305,8 +297,7 @@ 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++)
|
||||
markobject(g, g->mt[i]);
|
||||
|
@ -316,8 +307,7 @@ static void markmt(global_State *g)
|
|||
/*
|
||||
** mark all objects in list of being-finalized
|
||||
*/
|
||||
static void markbeingfnz(global_State *g)
|
||||
{
|
||||
static void markbeingfnz(global_State *g) {
|
||||
GCObject *o;
|
||||
for (o = g->tobefnz; o != NULL; o = gch(o)->next) {
|
||||
makewhite(g, o);
|
||||
|
@ -330,8 +320,7 @@ static void markbeingfnz(global_State *g)
|
|||
** mark all values stored in marked open upvalues. (See comment in
|
||||
** 'lstate.h'.)
|
||||
*/
|
||||
static void remarkupvals(global_State *g)
|
||||
{
|
||||
static void remarkupvals(global_State *g) {
|
||||
UpVal *uv;
|
||||
for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
|
||||
if (isgray(obj2gco(uv)))
|
||||
|
@ -344,8 +333,7 @@ static void remarkupvals(global_State *g)
|
|||
** mark root set and reset all gray lists, to start a new
|
||||
** incremental (or full) collection
|
||||
*/
|
||||
static void restartcollection(global_State *g)
|
||||
{
|
||||
static void restartcollection(global_State *g) {
|
||||
g->gray = g->grayagain = NULL;
|
||||
g->weak = g->allweak = g->ephemeron = NULL;
|
||||
markobject(g, g->mainthread);
|
||||
|
@ -363,8 +351,7 @@ static void restartcollection(global_State *g)
|
|||
** =======================================================
|
||||
*/
|
||||
|
||||
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 (do not
|
||||
traverse it just to check) */
|
||||
|
@ -387,8 +374,7 @@ static void traverseweakvalue(global_State *g, Table *h)
|
|||
}
|
||||
|
||||
|
||||
static int traverseephemeron(global_State *g, Table *h)
|
||||
{
|
||||
static int traverseephemeron(global_State *g, Table *h) {
|
||||
int marked = 0; /* true if an object is marked in this traversal */
|
||||
int hasclears = 0; /* true if table has white keys */
|
||||
int prop = 0; /* true if table has entry "white-key -> white-value" */
|
||||
|
@ -425,8 +411,7 @@ static int traverseephemeron(global_State *g, Table *h)
|
|||
}
|
||||
|
||||
|
||||
static void traversestrongtable(global_State *g, Table *h)
|
||||
{
|
||||
static void traversestrongtable(global_State *g, Table *h) {
|
||||
Node *n, *limit = gnodelast(h);
|
||||
int i;
|
||||
for (i = 0; i < h->sizearray; i++) /* traverse array part */
|
||||
|
@ -444,15 +429,14 @@ 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);
|
||||
markobject(g, h->metatable);
|
||||
if (mode && ttisstring(mode) && /* is there a weak mode? */
|
||||
((weakkey = strchr(svalue(mode), 'k')),
|
||||
(weakvalue = strchr(svalue(mode), 'v')),
|
||||
(weakkey || weakvalue))) { /* is really weak? */
|
||||
((weakkey = strchr(svalue(mode), 'k')),
|
||||
(weakvalue = strchr(svalue(mode), 'v')),
|
||||
(weakkey || weakvalue))) { /* is really weak? */
|
||||
black2gray(obj2gco(h)); /* keep table gray */
|
||||
if (!weakkey) /* strong keys? */
|
||||
traverseweakvalue(g, h);
|
||||
|
@ -467,8 +451,7 @@ static lu_mem traversetable(global_State *g, Table *h)
|
|||
}
|
||||
|
||||
|
||||
static int traverseproto(global_State *g, Proto *f)
|
||||
{
|
||||
static int traverseproto(global_State *g, Proto *f) {
|
||||
int i;
|
||||
if (f->cache && iswhite(obj2gco(f->cache)))
|
||||
f->cache = NULL; /* allow cache to be collected */
|
||||
|
@ -490,16 +473,14 @@ static int traverseproto(global_State *g, Proto *f)
|
|||
}
|
||||
|
||||
|
||||
static lu_mem traverseCclosure(global_State *g, CClosure *cl)
|
||||
{
|
||||
static lu_mem traverseCclosure(global_State *g, CClosure *cl) {
|
||||
int i;
|
||||
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
||||
markvalue(g, &cl->upvalue[i]);
|
||||
return sizeCclosure(cl->nupvalues);
|
||||
}
|
||||
|
||||
static lu_mem traverseLclosure(global_State *g, LClosure *cl)
|
||||
{
|
||||
static lu_mem traverseLclosure(global_State *g, LClosure *cl) {
|
||||
int i;
|
||||
markobject(g, cl->p); /* mark its prototype */
|
||||
for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */
|
||||
|
@ -508,8 +489,7 @@ static lu_mem traverseLclosure(global_State *g, LClosure *cl)
|
|||
}
|
||||
|
||||
|
||||
static lu_mem traversestack(global_State *g, lua_State *th)
|
||||
{
|
||||
static lu_mem traversestack(global_State *g, lua_State *th) {
|
||||
StkId o = th->stack;
|
||||
if (o == NULL)
|
||||
return 1; /* stack not completely built yet */
|
||||
|
@ -528,8 +508,7 @@ static lu_mem traversestack(global_State *g, lua_State *th)
|
|||
** traverse one gray object, turning it to black (except for threads,
|
||||
** which are always gray).
|
||||
*/
|
||||
static void propagatemark(global_State *g)
|
||||
{
|
||||
static void propagatemark(global_State *g) {
|
||||
lu_mem size;
|
||||
GCObject *o = g->gray;
|
||||
lua_assert(isgray(o));
|
||||
|
@ -576,14 +555,12 @@ static void propagatemark(global_State *g)
|
|||
}
|
||||
|
||||
|
||||
static void propagateall(global_State *g)
|
||||
{
|
||||
static void propagateall(global_State *g) {
|
||||
while (g->gray) propagatemark(g);
|
||||
}
|
||||
|
||||
|
||||
static void propagatelist(global_State *g, GCObject *l)
|
||||
{
|
||||
static void propagatelist(global_State *g, GCObject *l) {
|
||||
lua_assert(g->gray == NULL); /* no grays left */
|
||||
g->gray = l;
|
||||
propagateall(g); /* traverse all elements from 'l' */
|
||||
|
@ -594,8 +571,7 @@ static void propagatelist(global_State *g, GCObject *l)
|
|||
** lists when traversed, traverse the original lists to avoid traversing
|
||||
** twice the same table (which is not wrong, but inefficient)
|
||||
*/
|
||||
static void retraversegrays(global_State *g)
|
||||
{
|
||||
static void retraversegrays(global_State *g) {
|
||||
GCObject *weak = g->weak; /* save original lists */
|
||||
GCObject *grayagain = g->grayagain;
|
||||
GCObject *ephemeron = g->ephemeron;
|
||||
|
@ -607,8 +583,7 @@ static void retraversegrays(global_State *g)
|
|||
}
|
||||
|
||||
|
||||
static void convergeephemerons(global_State *g)
|
||||
{
|
||||
static void convergeephemerons(global_State *g) {
|
||||
int changed;
|
||||
do {
|
||||
GCObject *w;
|
||||
|
@ -639,8 +614,7 @@ static void convergeephemerons(global_State *g)
|
|||
** clear entries with unmarked keys from all weaktables in list 'l' up
|
||||
** to element 'f'
|
||||
*/
|
||||
static void clearkeys(global_State *g, GCObject *l, GCObject *f)
|
||||
{
|
||||
static void clearkeys(global_State *g, GCObject *l, GCObject *f) {
|
||||
for (; l != f; l = gco2t(l)->gclist) {
|
||||
Table *h = gco2t(l);
|
||||
Node *n, *limit = gnodelast(h);
|
||||
|
@ -658,8 +632,7 @@ static void clearkeys(global_State *g, GCObject *l, GCObject *f)
|
|||
** clear entries with unmarked values from all weaktables in list 'l' up
|
||||
** to element 'f'
|
||||
*/
|
||||
static void clearvalues(global_State *g, GCObject *l, GCObject *f)
|
||||
{
|
||||
static void clearvalues(global_State *g, GCObject *l, GCObject *f) {
|
||||
for (; l != f; l = gco2t(l)->gclist) {
|
||||
Table *h = gco2t(l);
|
||||
Node *n, *limit = gnodelast(h);
|
||||
|
@ -679,8 +652,7 @@ static void clearvalues(global_State *g, GCObject *l, GCObject *f)
|
|||
}
|
||||
|
||||
|
||||
static void freeobj(lua_State *L, GCObject *o)
|
||||
{
|
||||
static void freeobj(lua_State *L, GCObject *o) {
|
||||
switch (gch(o)->tt) {
|
||||
case LUA_TPROTO:
|
||||
luaF_freeproto(L, gco2p(o));
|
||||
|
@ -726,8 +698,7 @@ static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count);
|
|||
** sweep the (open) upvalues of a thread and resize its stack and
|
||||
** list of call-info structures.
|
||||
*/
|
||||
static void sweepthread(lua_State *L, lua_State *L1)
|
||||
{
|
||||
static void sweepthread(lua_State *L, lua_State *L1) {
|
||||
if (L1->stack == NULL) return; /* stack not completely built yet */
|
||||
sweepwholelist(L, &L1->openupval); /* sweep open upvalues */
|
||||
luaE_freeCI(L1); /* free extra CallInfo slots */
|
||||
|
@ -748,8 +719,7 @@ static void sweepthread(lua_State *L, lua_State *L1)
|
|||
** one will be old too.
|
||||
** When object is a thread, sweep its list of open upvalues too.
|
||||
*/
|
||||
static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count)
|
||||
{
|
||||
static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count) {
|
||||
global_State *g = G(L);
|
||||
int ow = otherwhite(g);
|
||||
int toclear, toset; /* bits to clear and to set in all live objects */
|
||||
|
@ -786,8 +756,7 @@ static GCObject **sweeplist(lua_State *L, GCObject **p, lu_mem count)
|
|||
/*
|
||||
** sweep a list until a live object (or end of list)
|
||||
*/
|
||||
static GCObject **sweeptolive(lua_State *L, GCObject **p, int *n)
|
||||
{
|
||||
static GCObject **sweeptolive(lua_State *L, GCObject **p, int *n) {
|
||||
GCObject **old = p;
|
||||
int i = 0;
|
||||
do {
|
||||
|
@ -807,8 +776,7 @@ static GCObject **sweeptolive(lua_State *L, GCObject **p, int *n)
|
|||
** =======================================================
|
||||
*/
|
||||
|
||||
static void checkSizes(lua_State *L)
|
||||
{
|
||||
static void checkSizes(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
if (g->gckind != KGC_EMERGENCY) { /* do not change sizes in emergency */
|
||||
int hs = g->strt.size / 2; /* half the size of the string table */
|
||||
|
@ -819,8 +787,7 @@ static void checkSizes(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static GCObject *udata2finalize(global_State *g)
|
||||
{
|
||||
static GCObject *udata2finalize(global_State *g) {
|
||||
GCObject *o = g->tobefnz; /* get first element */
|
||||
lua_assert(isfinalized(o));
|
||||
g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */
|
||||
|
@ -834,15 +801,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_call(L, L->top - 2, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
static void GCTM(lua_State *L, int propagateerrors)
|
||||
{
|
||||
static void GCTM(lua_State *L, int propagateerrors) {
|
||||
global_State *g = G(L);
|
||||
const TValue *tm;
|
||||
TValue v;
|
||||
|
@ -878,8 +843,7 @@ static void GCTM(lua_State *L, int propagateerrors)
|
|||
** move all unreachable objects (or 'all' objects) that need
|
||||
** finalization from list 'finobj' to list 'tobefnz' (to be finalized)
|
||||
*/
|
||||
static void separatetobefnz(lua_State *L, int all)
|
||||
{
|
||||
static void separatetobefnz(lua_State *L, int all) {
|
||||
global_State *g = G(L);
|
||||
GCObject **p = &g->finobj;
|
||||
GCObject *curr;
|
||||
|
@ -907,12 +871,11 @@ static void separatetobefnz(lua_State *L, int all)
|
|||
** 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 (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */
|
||||
isfinalized(o) || /* ... or is finalized... */
|
||||
gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
|
||||
isfinalized(o) || /* ... or is finalized... */
|
||||
gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
|
||||
return; /* nothing to be done */
|
||||
else { /* move 'o' to 'finobj' list */
|
||||
GCObject **p;
|
||||
|
@ -948,8 +911,7 @@ void luaC_checkfinalizer(lua_State *L, GCObject *o, Table *mt)
|
|||
** set a reasonable "time" to wait before starting a new GC cycle;
|
||||
** cycle will start when memory use hits threshold
|
||||
*/
|
||||
static void setpause(global_State *g, l_mem estimate)
|
||||
{
|
||||
static void setpause(global_State *g, l_mem estimate) {
|
||||
l_mem debt, threshold;
|
||||
estimate = estimate / PAUSEADJ; /* adjust 'estimate' */
|
||||
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
|
||||
|
@ -972,8 +934,7 @@ static void setpause(global_State *g, l_mem estimate)
|
|||
** of the real sweep.
|
||||
** Returns how many objects it swept.
|
||||
*/
|
||||
static int entersweep(lua_State *L)
|
||||
{
|
||||
static int entersweep(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int n = 0;
|
||||
g->gcstate = GCSsweepstring;
|
||||
|
@ -989,8 +950,7 @@ static int entersweep(lua_State *L)
|
|||
/*
|
||||
** change GC mode
|
||||
*/
|
||||
void luaC_changemode(lua_State *L, int mode)
|
||||
{
|
||||
void luaC_changemode(lua_State *L, int mode) {
|
||||
global_State *g = G(L);
|
||||
if (mode == g->gckind) return; /* nothing to change */
|
||||
if (mode == KGC_GEN) { /* change to generational mode */
|
||||
|
@ -1011,8 +971,7 @@ void luaC_changemode(lua_State *L, int mode)
|
|||
/*
|
||||
** call all pending finalizers
|
||||
*/
|
||||
static void callallpendingfinalizers(lua_State *L, int propagateerrors)
|
||||
{
|
||||
static void callallpendingfinalizers(lua_State *L, int propagateerrors) {
|
||||
global_State *g = G(L);
|
||||
while (g->tobefnz) {
|
||||
resetoldbit(g->tobefnz);
|
||||
|
@ -1021,8 +980,7 @@ static void callallpendingfinalizers(lua_State *L, int propagateerrors)
|
|||
}
|
||||
|
||||
|
||||
void luaC_freeallobjects(lua_State *L)
|
||||
{
|
||||
void luaC_freeallobjects(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int i;
|
||||
separatetobefnz(L, 1); /* separate all objects with finalizers */
|
||||
|
@ -1038,8 +996,7 @@ void luaC_freeallobjects(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static l_mem atomic(lua_State *L)
|
||||
{
|
||||
static l_mem atomic(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */
|
||||
GCObject *origweak, *origall;
|
||||
|
@ -1081,8 +1038,7 @@ static l_mem atomic(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static lu_mem singlestep(lua_State *L)
|
||||
{
|
||||
static lu_mem singlestep(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
switch (g->gcstate) {
|
||||
case GCSpause: {
|
||||
|
@ -1151,16 +1107,14 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
static void generationalcollection(lua_State *L)
|
||||
{
|
||||
static void generationalcollection(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(g->gcstate == GCSpropagate);
|
||||
if (g->GCestimate == 0) { /* signal for another major collection? */
|
||||
|
@ -1181,8 +1135,7 @@ static void generationalcollection(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void incstep(lua_State *L)
|
||||
{
|
||||
static void incstep(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
l_mem debt = g->GCdebt;
|
||||
int stepmul = g->gcstepmul;
|
||||
|
@ -1206,8 +1159,7 @@ static void incstep(lua_State *L)
|
|||
/*
|
||||
** performs a basic GC step
|
||||
*/
|
||||
void luaC_forcestep(lua_State *L)
|
||||
{
|
||||
void luaC_forcestep(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
int i;
|
||||
if (isgenerational(g)) generationalcollection(L);
|
||||
|
@ -1221,8 +1173,7 @@ void luaC_forcestep(lua_State *L)
|
|||
/*
|
||||
** performs a basic GC step only if collector is running
|
||||
*/
|
||||
void luaC_step(lua_State *L)
|
||||
{
|
||||
void luaC_step(lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
if (g->gcrunning) luaC_forcestep(L);
|
||||
else luaE_setdebt(g, -GCSTEPSIZE); /* avoid being called too often */
|
||||
|
@ -1234,8 +1185,7 @@ void luaC_step(lua_State *L)
|
|||
** performs a full GC cycle; if "isemergency", does not call
|
||||
** finalizers (which could change stack positions)
|
||||
*/
|
||||
void luaC_fullgc(lua_State *L, int isemergency)
|
||||
{
|
||||
void luaC_fullgc(lua_State *L, int isemergency) {
|
||||
global_State *g = G(L);
|
||||
int origkind = g->gckind;
|
||||
lua_assert(origkind != KGC_EMERGENCY);
|
||||
|
|
|
@ -49,8 +49,7 @@ static const luaL_Reg preloadedlibs[] = {
|
|||
};
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlibs(lua_State *L)
|
||||
{
|
||||
LUALIB_API void luaL_openlibs(lua_State *L) {
|
||||
const luaL_Reg *lib;
|
||||
/* call open functions from 'loadedlibs' and set results to global table */
|
||||
for (lib = loadedlibs; lib->func; lib++) {
|
||||
|
|
123
liblua/liolib.c
123
liblua/liolib.c
|
@ -126,8 +126,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);
|
||||
|
@ -141,8 +140,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)");
|
||||
|
@ -152,8 +150,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 (isclosed(p))
|
||||
luaL_error(L, "attempt to use a closed file");
|
||||
|
@ -167,8 +164,7 @@ static FILE *tofile(lua_State *L)
|
|||
** before opening the actual file; so, if there is a memory error, the
|
||||
** file is not left opened.
|
||||
*/
|
||||
static LStream *newprefile(lua_State *L)
|
||||
{
|
||||
static LStream *newprefile(lua_State *L) {
|
||||
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
|
||||
p->closef = NULL; /* mark file handle as 'closed' */
|
||||
luaL_setmetatable(L, LUA_FILEHANDLE);
|
||||
|
@ -176,8 +172,7 @@ static LStream *newprefile(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int aux_close(lua_State *L)
|
||||
{
|
||||
static int aux_close(lua_State *L) {
|
||||
LStream *p = tolstream(L);
|
||||
lua_CFunction cf = p->closef;
|
||||
p->closef = NULL; /* mark stream as closed */
|
||||
|
@ -185,8 +180,7 @@ static int aux_close(lua_State *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 standard output */
|
||||
tofile(L); /* make sure argument is an open stream */
|
||||
|
@ -194,8 +188,7 @@ static int io_close(lua_State *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 */
|
||||
|
@ -206,16 +199,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);
|
||||
int res = fclose(p->f);
|
||||
return luaL_fileresult(L, (res == 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;
|
||||
|
@ -223,8 +214,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 (p->f == NULL)
|
||||
|
@ -232,8 +222,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);
|
||||
|
@ -247,15 +236,13 @@ 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);
|
||||
return luaL_execresult(L, lua_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);
|
||||
|
@ -265,16 +252,14 @@ 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);
|
||||
p->f = tmpfile();
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -284,8 +269,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)
|
||||
|
@ -302,14 +286,12 @@ 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");
|
||||
}
|
||||
|
||||
|
@ -317,8 +299,7 @@ static int io_output(lua_State *L)
|
|||
static int io_readline(lua_State *L);
|
||||
|
||||
|
||||
static void aux_lines(lua_State *L, int toclose)
|
||||
{
|
||||
static void aux_lines(lua_State *L, int toclose) {
|
||||
int i;
|
||||
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
||||
/* ensure that arguments will fit here and into 'io_readline' stack */
|
||||
|
@ -331,16 +312,14 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
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? */
|
||||
|
@ -366,8 +345,7 @@ static int io_lines(lua_State *L)
|
|||
*/
|
||||
|
||||
|
||||
static int read_number(lua_State *L, FILE *f)
|
||||
{
|
||||
static int read_number(lua_State *L, FILE *f) {
|
||||
lua_Number d;
|
||||
if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
|
||||
lua_pushnumber(L, d);
|
||||
|
@ -379,8 +357,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);
|
||||
lua_pushlstring(L, NULL, 0);
|
||||
|
@ -388,8 +365,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;
|
||||
luaL_buffinit(L, &b);
|
||||
for (;;) {
|
||||
|
@ -413,8 +389,7 @@ static int read_line(lua_State *L, FILE *f, int chop)
|
|||
|
||||
#define MAX_SIZE_T (~(size_t)0)
|
||||
|
||||
static void read_all(lua_State *L, FILE *f)
|
||||
{
|
||||
static void read_all(lua_State *L, FILE *f) {
|
||||
size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */
|
||||
luaL_Buffer b;
|
||||
luaL_buffinit(L, &b);
|
||||
|
@ -430,8 +405,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;
|
||||
|
@ -444,8 +418,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 success;
|
||||
int n;
|
||||
|
@ -493,20 +466,17 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
|
@ -536,8 +506,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;
|
||||
for (; nargs--; arg++) {
|
||||
|
@ -556,22 +525,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);
|
||||
|
@ -590,8 +556,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);
|
||||
|
@ -603,14 +568,12 @@ static int f_setvbuf(lua_State *L)
|
|||
|
||||
|
||||
|
||||
static int io_flush(lua_State *L)
|
||||
{
|
||||
static int io_flush(lua_State *L) {
|
||||
return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
static int f_flush(lua_State *L)
|
||||
{
|
||||
static int f_flush(lua_State *L) {
|
||||
return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
|
||||
}
|
||||
|
||||
|
@ -651,8 +614,7 @@ static const luaL_Reg flib[] = {
|
|||
};
|
||||
|
||||
|
||||
static void createmeta(lua_State *L)
|
||||
{
|
||||
static void createmeta(lua_State *L) {
|
||||
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
||||
lua_pushvalue(L, -1); /* push metatable */
|
||||
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
||||
|
@ -664,8 +626,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 */
|
||||
lua_pushnil(L);
|
||||
|
@ -675,8 +636,7 @@ static int io_noclose(lua_State *L)
|
|||
|
||||
|
||||
static void createstdfile(lua_State *L, FILE *f, const char *k,
|
||||
const char *fname)
|
||||
{
|
||||
const char *fname) {
|
||||
LStream *p = newprefile(L);
|
||||
p->f = f;
|
||||
p->closef = &io_noclose;
|
||||
|
@ -688,8 +648,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 */
|
||||
|
|
|
@ -49,8 +49,7 @@ static const char *const luaX_tokens [] = {
|
|||
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;
|
||||
|
@ -63,8 +62,7 @@ static void save(LexState *ls, int c)
|
|||
}
|
||||
|
||||
|
||||
void luaX_init(lua_State *L)
|
||||
{
|
||||
void luaX_init(lua_State *L) {
|
||||
int i;
|
||||
for (i = 0; i < NUM_RESERVED; i++) {
|
||||
TString *ts = luaS_new(L, luaX_tokens[i]);
|
||||
|
@ -74,8 +72,7 @@ void luaX_init(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
const char *luaX_token2str(LexState *ls, int token)
|
||||
{
|
||||
const char *luaX_token2str(LexState *ls, int token) {
|
||||
if (token < FIRST_RESERVED) { /* single-byte symbols? */
|
||||
lua_assert(token == cast(unsigned char, token));
|
||||
return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) :
|
||||
|
@ -90,8 +87,7 @@ 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:
|
||||
|
@ -104,8 +100,7 @@ static const char *txtToken(LexState *ls, int token)
|
|||
}
|
||||
|
||||
|
||||
static l_noret lexerror(LexState *ls, const char *msg, int token)
|
||||
{
|
||||
static l_noret lexerror(LexState *ls, const char *msg, int token) {
|
||||
char buff[LUA_IDSIZE];
|
||||
luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
|
||||
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
|
||||
|
@ -115,8 +110,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);
|
||||
}
|
||||
|
||||
|
@ -126,8 +120,7 @@ l_noret luaX_syntaxerror(LexState *ls, const char *msg)
|
|||
** it will not be collected until the end of the function's compilation
|
||||
** (by that time it should be anchored in function's prototype)
|
||||
*/
|
||||
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;
|
||||
TValue *o; /* entry for `str' */
|
||||
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||
|
@ -148,8 +141,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' */
|
||||
|
@ -161,8 +153,7 @@ static void inclinenumber(LexState *ls)
|
|||
|
||||
|
||||
void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, TString *source,
|
||||
int firstchar)
|
||||
{
|
||||
int firstchar) {
|
||||
ls->decpoint = '.';
|
||||
ls->L = L;
|
||||
ls->current = firstchar;
|
||||
|
@ -187,8 +178,7 @@ void luaX_setinput(lua_State *L, LexState *ls, ZIO *z, TString *source,
|
|||
|
||||
|
||||
|
||||
static int check_next(LexState *ls, const char *set)
|
||||
{
|
||||
static int check_next(LexState *ls, const char *set) {
|
||||
if (ls->current == '\0' || !strchr(set, ls->current))
|
||||
return 0;
|
||||
save_and_next(ls);
|
||||
|
@ -199,8 +189,7 @@ static int check_next(LexState *ls, const char *set)
|
|||
/*
|
||||
** change all characters 'from' in buffer to 'to'
|
||||
*/
|
||||
static void buffreplace(LexState *ls, char from, char to)
|
||||
{
|
||||
static void buffreplace(LexState *ls, char from, char to) {
|
||||
size_t n = luaZ_bufflen(ls->buff);
|
||||
char *p = luaZ_buffer(ls->buff);
|
||||
while (n--)
|
||||
|
@ -221,8 +210,7 @@ static void buffreplace(LexState *ls, char from, char to)
|
|||
** in case of format error, try to change decimal point separator to
|
||||
** the one defined in the current locale and check again
|
||||
*/
|
||||
static void trydecpoint(LexState *ls, SemInfo *seminfo)
|
||||
{
|
||||
static void trydecpoint(LexState *ls, SemInfo *seminfo) {
|
||||
char old = ls->decpoint;
|
||||
ls->decpoint = getlocaledecpoint();
|
||||
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
|
||||
|
@ -239,8 +227,7 @@ static void trydecpoint(LexState *ls, SemInfo *seminfo)
|
|||
** this function is quite liberal in what it accepts, as 'luaO_str2d'
|
||||
** will reject ill-formed numerals.
|
||||
*/
|
||||
static void read_numeral(LexState *ls, SemInfo *seminfo)
|
||||
{
|
||||
static void read_numeral(LexState *ls, SemInfo *seminfo) {
|
||||
const char *expo = "Ee";
|
||||
int first = ls->current;
|
||||
lua_assert(lisdigit(ls->current));
|
||||
|
@ -265,8 +252,7 @@ static void read_numeral(LexState *ls, SemInfo *seminfo)
|
|||
** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
|
||||
** -1 if sequence is malformed
|
||||
*/
|
||||
static int skip_sep(LexState *ls)
|
||||
{
|
||||
static int skip_sep(LexState *ls) {
|
||||
int count = 0;
|
||||
int s = ls->current;
|
||||
lua_assert(s == '[' || s == ']');
|
||||
|
@ -279,8 +265,7 @@ static int skip_sep(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void read_long_string(LexState *ls, SemInfo *seminfo, int sep)
|
||||
{
|
||||
static void read_long_string(LexState *ls, SemInfo *seminfo, int sep) {
|
||||
save_and_next(ls); /* skip 2nd `[' */
|
||||
if (currIsNewline(ls)) /* string starts with a newline? */
|
||||
inclinenumber(ls); /* skip it */
|
||||
|
@ -317,8 +302,7 @@ endloop:
|
|||
}
|
||||
|
||||
|
||||
static void escerror(LexState *ls, int *c, int n, const char *msg)
|
||||
{
|
||||
static void escerror(LexState *ls, int *c, int n, const char *msg) {
|
||||
int i;
|
||||
luaZ_resetbuffer(ls->buff); /* prepare error message */
|
||||
save(ls, '\\');
|
||||
|
@ -328,8 +312,7 @@ static void escerror(LexState *ls, int *c, int n, const char *msg)
|
|||
}
|
||||
|
||||
|
||||
static int readhexaesc(LexState *ls)
|
||||
{
|
||||
static int readhexaesc(LexState *ls) {
|
||||
int c[3], i; /* keep input for error message */
|
||||
int r = 0; /* result accumulator */
|
||||
c[0] = 'x'; /* for error message */
|
||||
|
@ -343,8 +326,7 @@ static int readhexaesc(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static int readdecesc(LexState *ls)
|
||||
{
|
||||
static int readdecesc(LexState *ls) {
|
||||
int c[3], i;
|
||||
int r = 0; /* result accumulator */
|
||||
for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
|
||||
|
@ -358,8 +340,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) {
|
||||
|
@ -443,8 +424,7 @@ no_save:
|
|||
}
|
||||
|
||||
|
||||
static int llex(LexState *ls, SemInfo *seminfo)
|
||||
{
|
||||
static int llex(LexState *ls, SemInfo *seminfo) {
|
||||
luaZ_resetbuffer(ls->buff);
|
||||
for (;;) {
|
||||
switch (ls->current) {
|
||||
|
@ -567,8 +547,7 @@ 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 */
|
||||
|
@ -578,8 +557,7 @@ void luaX_next(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
|
|
@ -23,94 +23,79 @@
|
|||
|
||||
|
||||
|
||||
static int math_abs(lua_State *L)
|
||||
{
|
||||
static int math_abs(lua_State *L) {
|
||||
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_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_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_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_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_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_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_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_atan2(lua_State *L)
|
||||
{
|
||||
static int math_atan2(lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),
|
||||
luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_ceil(lua_State *L)
|
||||
{
|
||||
static int math_ceil(lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_floor(lua_State *L)
|
||||
{
|
||||
static int math_floor(lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_fmod(lua_State *L)
|
||||
{
|
||||
static int math_fmod(lua_State *L) {
|
||||
lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
|
||||
luaL_checknumber(L, 2)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_modf(lua_State *L)
|
||||
{
|
||||
static int math_modf(lua_State *L) {
|
||||
lua_Number ip;
|
||||
lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);
|
||||
lua_pushnumber(L, ip);
|
||||
|
@ -118,22 +103,19 @@ static int math_modf(lua_State *L)
|
|||
return 2;
|
||||
}
|
||||
|
||||
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_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_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))
|
||||
|
@ -148,41 +130,35 @@ static int math_log(lua_State *L)
|
|||
}
|
||||
|
||||
#if defined(LUA_COMPAT_LOG10)
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
|
||||
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) / RADIANS_PER_DEGREE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int math_rad(lua_State *L)
|
||||
{
|
||||
static int math_rad(lua_State *L) {
|
||||
lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
|
||||
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 = luaL_checkint(L, 2);
|
||||
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
|
||||
|
@ -191,8 +167,7 @@ static int math_ldexp(lua_State *L)
|
|||
|
||||
|
||||
|
||||
static int math_min(lua_State *L)
|
||||
{
|
||||
static int math_min(lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
lua_Number dmin = luaL_checknumber(L, 1);
|
||||
int i;
|
||||
|
@ -206,8 +181,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 */
|
||||
lua_Number dmax = luaL_checknumber(L, 1);
|
||||
int i;
|
||||
|
@ -221,8 +195,7 @@ static int math_max(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int math_random(lua_State *L)
|
||||
{
|
||||
static int math_random(lua_State *L) {
|
||||
/* the `%' avoids the (rare) case of r==1, and is needed also because on
|
||||
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
|
||||
lua_Number r = (lua_Number)(rand() % RAND_MAX) / (lua_Number)RAND_MAX;
|
||||
|
@ -251,8 +224,7 @@ static int math_random(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int math_randomseed(lua_State *L)
|
||||
{
|
||||
static int math_randomseed(lua_State *L) {
|
||||
srand(luaL_checkunsigned(L, 1));
|
||||
(void)rand(); /* discard first value to avoid undesirable correlations */
|
||||
return 0;
|
||||
|
@ -297,8 +269,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");
|
||||
|
|
|
@ -44,8 +44,7 @@
|
|||
|
||||
|
||||
void *luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems,
|
||||
int limit, const char *what)
|
||||
{
|
||||
int limit, const char *what) {
|
||||
void *newblock;
|
||||
int newsize;
|
||||
if (*size >= limit / 2) { /* cannot double it? */
|
||||
|
@ -63,8 +62,7 @@ void *luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems,
|
|||
}
|
||||
|
||||
|
||||
l_noret luaM_toobig(lua_State *L)
|
||||
{
|
||||
l_noret luaM_toobig(lua_State *L) {
|
||||
luaG_runerror(L, "memory allocation error: block too big");
|
||||
}
|
||||
|
||||
|
@ -73,8 +71,7 @@ l_noret luaM_toobig(lua_State *L)
|
|||
/*
|
||||
** 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);
|
||||
size_t realosize = (block) ? osize : 0;
|
||||
|
|
118
liblua/loadlib.c
118
liblua/loadlib.c
|
@ -126,22 +126,19 @@ static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym);
|
|||
|
||||
#include <dlfcn.h>
|
||||
|
||||
static void ll_unloadlib(void *lib)
|
||||
{
|
||||
static void ll_unloadlib(void *lib) {
|
||||
dlclose(lib);
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load(lua_State *L, const char *path, int seeglb)
|
||||
{
|
||||
static void *ll_load(lua_State *L, const char *path, int seeglb) {
|
||||
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
||||
if (lib == NULL) lua_pushstring(L, dlerror());
|
||||
return lib;
|
||||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
|
||||
{
|
||||
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
|
||||
if (f == NULL) lua_pushstring(L, dlerror());
|
||||
return f;
|
||||
|
@ -168,8 +165,7 @@ static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
|
|||
#endif
|
||||
|
||||
|
||||
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);
|
||||
|
@ -184,8 +180,7 @@ 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,
|
||||
|
@ -195,14 +190,12 @@ static void pusherror(lua_State *L)
|
|||
lua_pushfstring(L, "system error %d\n", error);
|
||||
}
|
||||
|
||||
static void ll_unloadlib(void *lib)
|
||||
{
|
||||
static void ll_unloadlib(void *lib) {
|
||||
FreeLibrary((HMODULE)lib);
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load(lua_State *L, const char *path, int seeglb)
|
||||
{
|
||||
static void *ll_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);
|
||||
|
@ -210,8 +203,7 @@ static void *ll_load(lua_State *L, const char *path, int seeglb)
|
|||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
|
||||
{
|
||||
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) {
|
||||
lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
|
||||
if (f == NULL) pusherror(L);
|
||||
return f;
|
||||
|
@ -234,14 +226,12 @@ static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
|
|||
#define DLMSG "dynamic libraries not enabled; check your Lua installation"
|
||||
|
||||
|
||||
static void ll_unloadlib(void *lib)
|
||||
{
|
||||
static void ll_unloadlib(void *lib) {
|
||||
(void)(lib); /* not used */
|
||||
}
|
||||
|
||||
|
||||
static void *ll_load(lua_State *L, const char *path, int seeglb)
|
||||
{
|
||||
static void *ll_load(lua_State *L, const char *path, int seeglb) {
|
||||
(void)(path);
|
||||
(void)(seeglb); /* not used */
|
||||
lua_pushliteral(L, DLMSG);
|
||||
|
@ -249,8 +239,7 @@ static void *ll_load(lua_State *L, const char *path, int seeglb)
|
|||
}
|
||||
|
||||
|
||||
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
|
||||
{
|
||||
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) {
|
||||
(void)(lib);
|
||||
(void)(sym); /* not used */
|
||||
lua_pushliteral(L, DLMSG);
|
||||
|
@ -261,8 +250,7 @@ static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
|
|||
#endif
|
||||
|
||||
|
||||
static void *ll_checkclib(lua_State *L, const char *path)
|
||||
{
|
||||
static void *ll_checkclib(lua_State *L, const char *path) {
|
||||
void *plib;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_getfield(L, -1, path);
|
||||
|
@ -272,8 +260,7 @@ static void *ll_checkclib(lua_State *L, const char *path)
|
|||
}
|
||||
|
||||
|
||||
static void ll_addtoclib(lua_State *L, const char *path, void *plib)
|
||||
{
|
||||
static void ll_addtoclib(lua_State *L, const char *path, void *plib) {
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_pushlightuserdata(L, plib);
|
||||
lua_pushvalue(L, -1);
|
||||
|
@ -287,8 +274,7 @@ static void ll_addtoclib(lua_State *L, const char *path, void *plib)
|
|||
** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib
|
||||
** handles in list CLIBS
|
||||
*/
|
||||
static int gctm(lua_State *L)
|
||||
{
|
||||
static int gctm(lua_State *L) {
|
||||
int n = luaL_len(L, 1);
|
||||
for (; n >= 1; n--) { /* for each handle, in reverse order */
|
||||
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
|
||||
|
@ -299,8 +285,7 @@ static int gctm(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int ll_loadfunc(lua_State *L, const char *path, const char *sym)
|
||||
{
|
||||
static int ll_loadfunc(lua_State *L, const char *path, const char *sym) {
|
||||
void *reg = ll_checkclib(L, path); /* check loaded C libraries */
|
||||
if (reg == NULL) { /* must load library? */
|
||||
reg = ll_load(L, path, *sym == '*');
|
||||
|
@ -320,8 +305,7 @@ static int ll_loadfunc(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 = ll_loadfunc(L, path, init);
|
||||
|
@ -344,8 +328,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);
|
||||
|
@ -353,8 +336,7 @@ static int readable(const char *filename)
|
|||
}
|
||||
|
||||
|
||||
static const char *pushnexttemplate(lua_State *L, const char *path)
|
||||
{
|
||||
static const char *pushnexttemplate(lua_State *L, const char *path) {
|
||||
const char *l;
|
||||
while (*path == *LUA_PATH_SEP) path++; /* skip separators */
|
||||
if (*path == '\0') return NULL; /* no more templates */
|
||||
|
@ -368,8 +350,7 @@ static const char *pushnexttemplate(lua_State *L, const char *path)
|
|||
static const char *searchpath(lua_State *L, const char *name,
|
||||
const char *path,
|
||||
const char *sep,
|
||||
const char *dirsep)
|
||||
{
|
||||
const char *dirsep) {
|
||||
luaL_Buffer msg; /* to build error message */
|
||||
luaL_buffinit(L, &msg);
|
||||
if (*sep != '\0') /* non-empty separator? */
|
||||
|
@ -389,8 +370,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, "."),
|
||||
|
@ -406,8 +386,7 @@ static int ll_searchpath(lua_State *L)
|
|||
|
||||
static const char *findfile(lua_State *L, const char *name,
|
||||
const char *pname,
|
||||
const char *dirsep)
|
||||
{
|
||||
const char *dirsep) {
|
||||
const char *path;
|
||||
lua_getfield(L, lua_upvalueindex(1), pname);
|
||||
path = lua_tostring(L, -1);
|
||||
|
@ -417,8 +396,7 @@ 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 (stat) { /* module loaded successfully? */
|
||||
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
||||
return 2; /* return open function and file name */
|
||||
|
@ -429,8 +407,7 @@ static int checkload(lua_State *L, int stat, const char *filename)
|
|||
}
|
||||
|
||||
|
||||
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);
|
||||
|
@ -439,8 +416,7 @@ static int searcher_Lua(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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 *funcname;
|
||||
const char *mark;
|
||||
modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
|
||||
|
@ -458,8 +434,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 */
|
||||
|
@ -467,8 +442,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, '.');
|
||||
|
@ -491,8 +465,7 @@ 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, "_PRELOAD");
|
||||
lua_getfield(L, -1, name);
|
||||
|
@ -502,8 +475,7 @@ static int searcher_preload(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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 */
|
||||
luaL_buffinit(L, &msg);
|
||||
|
@ -532,8 +504,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, "_LOADED");
|
||||
|
@ -571,12 +542,11 @@ static int ll_require(lua_State *L)
|
|||
/*
|
||||
** changes the environment variable of calling function
|
||||
*/
|
||||
static void set_env(lua_State *L)
|
||||
{
|
||||
static void set_env(lua_State *L) {
|
||||
lua_Debug ar;
|
||||
if (lua_getstack(L, 1, &ar) == 0 ||
|
||||
lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
|
||||
lua_iscfunction(L, -1))
|
||||
lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
|
||||
lua_iscfunction(L, -1))
|
||||
luaL_error(L, LUA_QL("module") " not called from a Lua function");
|
||||
lua_pushvalue(L, -2); /* copy new environment table to top */
|
||||
lua_setupvalue(L, -2, 1);
|
||||
|
@ -584,8 +554,7 @@ static void set_env(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void dooptions(lua_State *L, int n)
|
||||
{
|
||||
static void dooptions(lua_State *L, int n) {
|
||||
int i;
|
||||
for (i = 2; i <= n; i++) {
|
||||
if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */
|
||||
|
@ -597,8 +566,7 @@ static void dooptions(lua_State *L, int n)
|
|||
}
|
||||
|
||||
|
||||
static void modinit(lua_State *L, const char *modname)
|
||||
{
|
||||
static void modinit(lua_State *L, const char *modname) {
|
||||
const char *dot;
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -2, "_M"); /* module._M = module */
|
||||
|
@ -613,8 +581,7 @@ static void modinit(lua_State *L, const char *modname)
|
|||
}
|
||||
|
||||
|
||||
static int ll_module(lua_State *L)
|
||||
{
|
||||
static int ll_module(lua_State *L) {
|
||||
const char *modname = luaL_checkstring(L, 1);
|
||||
int lastarg = lua_gettop(L); /* last parameter */
|
||||
luaL_pushmodule(L, modname, 1); /* get/create module table */
|
||||
|
@ -633,8 +600,7 @@ static int ll_module(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int ll_seeall(lua_State *L)
|
||||
{
|
||||
static int ll_seeall(lua_State *L) {
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
if (!lua_getmetatable(L, 1)) {
|
||||
lua_createtable(L, 0, 1); /* create new metatable */
|
||||
|
@ -658,8 +624,7 @@ static int ll_seeall(lua_State *L)
|
|||
/*
|
||||
** 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);
|
||||
|
@ -669,8 +634,7 @@ static int noenv(lua_State *L)
|
|||
|
||||
|
||||
static void setpath(lua_State *L, const char *fieldname, const char *envname1,
|
||||
const char *envname2, const char *def)
|
||||
{
|
||||
const char *envname2, const char *def) {
|
||||
const char *path = getenv(envname1);
|
||||
if (path == NULL) /* no environment variable? */
|
||||
path = getenv(envname2); /* try alternative name */
|
||||
|
@ -707,8 +671,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, searcher_C, searcher_Croot, NULL};
|
||||
int i;
|
||||
|
@ -723,8 +686,7 @@ static void createsearcherstable(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
LUAMOD_API int luaopen_package(lua_State *L)
|
||||
{
|
||||
LUAMOD_API int luaopen_package(lua_State *L) {
|
||||
/* create table CLIBS to keep track of loaded C libraries */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);
|
||||
lua_createtable(L, 0, 1); /* metatable for CLIBS */
|
||||
|
|
|
@ -33,8 +33,7 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
|||
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
||||
** eeeee != 0 and (xxx) otherwise.
|
||||
*/
|
||||
int luaO_int2fb(unsigned int x)
|
||||
{
|
||||
int luaO_int2fb(unsigned int x) {
|
||||
int e = 0; /* exponent */
|
||||
if (x < 8) return x;
|
||||
while (x >= 0x10) {
|
||||
|
@ -46,16 +45,14 @@ int luaO_int2fb(unsigned int x)
|
|||
|
||||
|
||||
/* converts back */
|
||||
int luaO_fb2int(int x)
|
||||
{
|
||||
int luaO_fb2int(int x) {
|
||||
int e = (x >> 3) & 0x1f;
|
||||
if (e == 0) return x;
|
||||
else return ((x & 7) + 8) << (e - 1);
|
||||
}
|
||||
|
||||
|
||||
int luaO_ceillog2(unsigned int x)
|
||||
{
|
||||
int luaO_ceillog2(unsigned int x) {
|
||||
static const lu_byte log_2[256] = {
|
||||
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,
|
||||
|
@ -73,8 +70,7 @@ int luaO_ceillog2(unsigned int x)
|
|||
}
|
||||
|
||||
|
||||
lua_Number luaO_arith(int op, lua_Number v1, lua_Number v2)
|
||||
{
|
||||
lua_Number luaO_arith(int op, lua_Number v1, lua_Number v2) {
|
||||
switch (op) {
|
||||
case LUA_OPADD:
|
||||
return luai_numadd(NULL, v1, v2);
|
||||
|
@ -97,8 +93,7 @@ lua_Number luaO_arith(int op, lua_Number v1, lua_Number v2)
|
|||
}
|
||||
|
||||
|
||||
int luaO_hexavalue(int c)
|
||||
{
|
||||
int luaO_hexavalue(int c) {
|
||||
if (lisdigit(c)) return c - '0';
|
||||
else return ltolower(c) - 'a' + 10;
|
||||
}
|
||||
|
@ -109,16 +104,14 @@ int luaO_hexavalue(int c)
|
|||
#include <math.h>
|
||||
|
||||
|
||||
static int isneg(const char **s)
|
||||
{
|
||||
static int isneg(const char **s) {
|
||||
if (**s == '-') { (*s)++; return 1; }
|
||||
else if (**s == '+')(*s)++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static lua_Number readhexa(const char **s, lua_Number r, int *count)
|
||||
{
|
||||
static lua_Number readhexa(const char **s, lua_Number r, int *count) {
|
||||
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
|
||||
r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
|
||||
(*count)++;
|
||||
|
@ -131,8 +124,7 @@ static lua_Number readhexa(const char **s, lua_Number r, int *count)
|
|||
** convert an 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) {
|
||||
lua_Number r = 0.0;
|
||||
int e = 0, i = 0;
|
||||
int neg = 0; /* 1 if number is negative */
|
||||
|
@ -172,8 +164,7 @@ ret:
|
|||
#endif
|
||||
|
||||
|
||||
int luaO_str2d(const char *s, size_t len, lua_Number *result)
|
||||
{
|
||||
int luaO_str2d(const char *s, size_t len, lua_Number *result) {
|
||||
char *endptr;
|
||||
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
|
||||
return 0;
|
||||
|
@ -188,15 +179,13 @@ int luaO_str2d(const char *s, size_t len, lua_Number *result)
|
|||
|
||||
|
||||
|
||||
static void pushstr(lua_State *L, const char *str, size_t l)
|
||||
{
|
||||
static void pushstr(lua_State *L, const char *str, size_t l) {
|
||||
setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
|
||||
}
|
||||
|
||||
|
||||
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
|
||||
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) {
|
||||
int n = 0;
|
||||
for (;;) {
|
||||
const char *e = strchr(fmt, '%');
|
||||
|
@ -250,8 +239,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);
|
||||
|
@ -270,8 +258,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 bufflen)
|
||||
{
|
||||
void luaO_chunkid(char *out, const char *source, size_t bufflen) {
|
||||
size_t l = strlen(source);
|
||||
if (*source == '=') { /* 'literal' source */
|
||||
if (l <= bufflen) /* small enough? */
|
||||
|
|
|
@ -77,8 +77,7 @@
|
|||
|
||||
|
||||
|
||||
static int os_execute(lua_State *L)
|
||||
{
|
||||
static int os_execute(lua_State *L) {
|
||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||
int stat = system(cmd);
|
||||
if (cmd != NULL)
|
||||
|
@ -90,23 +89,20 @@ 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);
|
||||
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);
|
||||
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
static int os_tmpname(lua_State *L)
|
||||
{
|
||||
static int os_tmpname(lua_State *L) {
|
||||
char buff[LUA_TMPNAMBUFSIZE];
|
||||
int err;
|
||||
lua_tmpnam(buff, err);
|
||||
|
@ -117,15 +113,13 @@ 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)
|
||||
{
|
||||
static int os_clock(lua_State *L) {
|
||||
lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)CLOCKS_PER_SEC);
|
||||
return 1;
|
||||
}
|
||||
|
@ -139,22 +133,19 @@ static int os_clock(lua_State *L)
|
|||
** =======================================================
|
||||
*/
|
||||
|
||||
static void setfield(lua_State *L, const char *key, int value)
|
||||
{
|
||||
static void setfield(lua_State *L, const char *key, int value) {
|
||||
lua_pushinteger(L, value);
|
||||
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);
|
||||
lua_setfield(L, -2, key);
|
||||
}
|
||||
|
||||
static int getboolfield(lua_State *L, const char *key)
|
||||
{
|
||||
static int getboolfield(lua_State *L, const char *key) {
|
||||
int res;
|
||||
lua_getfield(L, -1, key);
|
||||
res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
|
||||
|
@ -163,8 +154,7 @@ static int getboolfield(lua_State *L, const char *key)
|
|||
}
|
||||
|
||||
|
||||
static int getfield(lua_State *L, const char *key, int d)
|
||||
{
|
||||
static int getfield(lua_State *L, const char *key, int d) {
|
||||
int res, isnum;
|
||||
lua_getfield(L, -1, key);
|
||||
res = (int)lua_tointegerx(L, -1, &isnum);
|
||||
|
@ -178,8 +168,7 @@ static int getfield(lua_State *L, const char *key, int d)
|
|||
}
|
||||
|
||||
|
||||
static const char *checkoption(lua_State *L, const char *conv, char *buff)
|
||||
{
|
||||
static const char *checkoption(lua_State *L, const char *conv, char *buff) {
|
||||
static const char *const options[] = LUA_STRFTIMEOPTIONS;
|
||||
unsigned int i;
|
||||
for (i = 0; i < sizeof(options) / sizeof(options[0]); i += 2) {
|
||||
|
@ -202,8 +191,7 @@ static const char *checkoption(lua_State *L, const char *conv, char *buff)
|
|||
}
|
||||
|
||||
|
||||
static int os_date(lua_State *L)
|
||||
{
|
||||
static int os_date(lua_State *L) {
|
||||
const char *s = luaL_optstring(L, 1, "%c");
|
||||
time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
|
||||
struct tm tmr, *stm;
|
||||
|
@ -247,8 +235,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 */
|
||||
|
@ -273,8 +260,7 @@ static int os_time(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int os_difftime(lua_State *L)
|
||||
{
|
||||
static int os_difftime(lua_State *L) {
|
||||
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
|
||||
(time_t)(luaL_optnumber(L, 2, 0))));
|
||||
return 1;
|
||||
|
@ -283,8 +269,7 @@ 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
|
||||
};
|
||||
|
@ -298,8 +283,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);
|
||||
|
@ -331,8 +315,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;
|
||||
}
|
||||
|
|
265
liblua/lparser.c
265
liblua/lparser.c
|
@ -57,8 +57,7 @@ static void statement(LexState *ls);
|
|||
static void expr(LexState *ls, expdesc *v);
|
||||
|
||||
|
||||
static void anchor_token(LexState *ls)
|
||||
{
|
||||
static void anchor_token(LexState *ls) {
|
||||
/* last token from outer function must be EOS */
|
||||
lua_assert(ls->fs != NULL || ls->t.token == TK_EOS);
|
||||
if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
|
||||
|
@ -69,22 +68,19 @@ static void anchor_token(LexState *ls)
|
|||
|
||||
|
||||
/* semantic error */
|
||||
static l_noret semerror(LexState *ls, const char *msg)
|
||||
{
|
||||
static l_noret semerror(LexState *ls, const char *msg) {
|
||||
ls->t.token = 0; /* remove 'near to' from final message */
|
||||
luaX_syntaxerror(ls, msg);
|
||||
}
|
||||
|
||||
|
||||
static l_noret error_expected(LexState *ls, int token)
|
||||
{
|
||||
static l_noret error_expected(LexState *ls, int token) {
|
||||
luaX_syntaxerror(ls,
|
||||
luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
|
||||
}
|
||||
|
||||
|
||||
static l_noret errorlimit(FuncState *fs, int limit, const char *what)
|
||||
{
|
||||
static l_noret errorlimit(FuncState *fs, int limit, const char *what) {
|
||||
lua_State *L = fs->ls->L;
|
||||
const char *msg;
|
||||
int line = fs->f->linedefined;
|
||||
|
@ -97,14 +93,12 @@ static l_noret errorlimit(FuncState *fs, int limit, const char *what)
|
|||
}
|
||||
|
||||
|
||||
static void checklimit(FuncState *fs, int v, int l, const char *what)
|
||||
{
|
||||
static void checklimit(FuncState *fs, int v, int l, const char *what) {
|
||||
if (v > l) errorlimit(fs, l, what);
|
||||
}
|
||||
|
||||
|
||||
static int testnext(LexState *ls, int c)
|
||||
{
|
||||
static int testnext(LexState *ls, int c) {
|
||||
if (ls->t.token == c) {
|
||||
luaX_next(ls);
|
||||
return 1;
|
||||
|
@ -112,15 +106,13 @@ static int testnext(LexState *ls, int c)
|
|||
}
|
||||
|
||||
|
||||
static void check(LexState *ls, int c)
|
||||
{
|
||||
static void check(LexState *ls, int c) {
|
||||
if (ls->t.token != c)
|
||||
error_expected(ls, c);
|
||||
}
|
||||
|
||||
|
||||
static void checknext(LexState *ls, int c)
|
||||
{
|
||||
static void checknext(LexState *ls, int c) {
|
||||
check(ls, c);
|
||||
luaX_next(ls);
|
||||
}
|
||||
|
@ -130,8 +122,7 @@ static void checknext(LexState *ls, int c)
|
|||
|
||||
|
||||
|
||||
static void check_match(LexState *ls, int what, int who, int where)
|
||||
{
|
||||
static void check_match(LexState *ls, int what, int who, int where) {
|
||||
if (!testnext(ls, what)) {
|
||||
if (where == ls->linenumber)
|
||||
error_expected(ls, what);
|
||||
|
@ -144,8 +135,7 @@ static void check_match(LexState *ls, int what, int who, int where)
|
|||
}
|
||||
|
||||
|
||||
static TString *str_checkname(LexState *ls)
|
||||
{
|
||||
static TString *str_checkname(LexState *ls) {
|
||||
TString *ts;
|
||||
check(ls, TK_NAME);
|
||||
ts = ls->t.seminfo.ts;
|
||||
|
@ -154,28 +144,24 @@ static TString *str_checkname(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void init_exp(expdesc *e, expkind k, int i)
|
||||
{
|
||||
static void init_exp(expdesc *e, expkind k, int i) {
|
||||
e->f = e->t = NO_JUMP;
|
||||
e->k = k;
|
||||
e->u.info = i;
|
||||
}
|
||||
|
||||
|
||||
static void codestring(LexState *ls, expdesc *e, TString *s)
|
||||
{
|
||||
static void codestring(LexState *ls, expdesc *e, TString *s) {
|
||||
init_exp(e, VK, luaK_stringK(ls->fs, s));
|
||||
}
|
||||
|
||||
|
||||
static void checkname(LexState *ls, expdesc *e)
|
||||
{
|
||||
static void checkname(LexState *ls, expdesc *e) {
|
||||
codestring(ls, e, str_checkname(ls));
|
||||
}
|
||||
|
||||
|
||||
static int registerlocalvar(LexState *ls, TString *varname)
|
||||
{
|
||||
static int registerlocalvar(LexState *ls, TString *varname) {
|
||||
FuncState *fs = ls->fs;
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizelocvars;
|
||||
|
@ -188,8 +174,7 @@ static int registerlocalvar(LexState *ls, TString *varname)
|
|||
}
|
||||
|
||||
|
||||
static void new_localvar(LexState *ls, TString *name)
|
||||
{
|
||||
static void new_localvar(LexState *ls, TString *name) {
|
||||
FuncState *fs = ls->fs;
|
||||
Dyndata *dyd = ls->dyd;
|
||||
int reg = registerlocalvar(ls, name);
|
||||
|
@ -201,8 +186,7 @@ static void new_localvar(LexState *ls, TString *name)
|
|||
}
|
||||
|
||||
|
||||
static void new_localvarliteral_(LexState *ls, const char *name, size_t sz)
|
||||
{
|
||||
static void new_localvarliteral_(LexState *ls, const char *name, size_t sz) {
|
||||
new_localvar(ls, luaX_newstring(ls, name, sz));
|
||||
}
|
||||
|
||||
|
@ -210,16 +194,14 @@ static void new_localvarliteral_(LexState *ls, const char *name, size_t sz)
|
|||
new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1)
|
||||
|
||||
|
||||
static LocVar *getlocvar(FuncState *fs, int i)
|
||||
{
|
||||
static LocVar *getlocvar(FuncState *fs, int i) {
|
||||
int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx;
|
||||
lua_assert(idx < fs->nlocvars);
|
||||
return &fs->f->locvars[idx];
|
||||
}
|
||||
|
||||
|
||||
static void adjustlocalvars(LexState *ls, int nvars)
|
||||
{
|
||||
static void adjustlocalvars(LexState *ls, int nvars) {
|
||||
FuncState *fs = ls->fs;
|
||||
fs->nactvar = cast_byte(fs->nactvar + nvars);
|
||||
for (; nvars; nvars--) {
|
||||
|
@ -228,16 +210,14 @@ static void adjustlocalvars(LexState *ls, int nvars)
|
|||
}
|
||||
|
||||
|
||||
static void removevars(FuncState *fs, int tolevel)
|
||||
{
|
||||
static void removevars(FuncState *fs, int tolevel) {
|
||||
fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
|
||||
while (fs->nactvar > tolevel)
|
||||
getlocvar(fs, --fs->nactvar)->endpc = fs->pc;
|
||||
}
|
||||
|
||||
|
||||
static int searchupvalue(FuncState *fs, TString *name)
|
||||
{
|
||||
static int searchupvalue(FuncState *fs, TString *name) {
|
||||
int i;
|
||||
Upvaldesc *up = fs->f->upvalues;
|
||||
for (i = 0; i < fs->nups; i++) {
|
||||
|
@ -247,8 +227,7 @@ static int searchupvalue(FuncState *fs, TString *name)
|
|||
}
|
||||
|
||||
|
||||
static int newupvalue(FuncState *fs, TString *name, expdesc *v)
|
||||
{
|
||||
static int newupvalue(FuncState *fs, TString *name, expdesc *v) {
|
||||
Proto *f = fs->f;
|
||||
int oldsize = f->sizeupvalues;
|
||||
checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
|
||||
|
@ -263,8 +242,7 @@ static int newupvalue(FuncState *fs, TString *name, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static int searchvar(FuncState *fs, TString *n)
|
||||
{
|
||||
static int searchvar(FuncState *fs, TString *n) {
|
||||
int i;
|
||||
for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
|
||||
if (luaS_eqstr(n, getlocvar(fs, i)->varname))
|
||||
|
@ -278,8 +256,7 @@ static int searchvar(FuncState *fs, TString *n)
|
|||
Mark block where variable at given level was defined
|
||||
(to emit close instructions later).
|
||||
*/
|
||||
static void markupval(FuncState *fs, int level)
|
||||
{
|
||||
static void markupval(FuncState *fs, int level) {
|
||||
BlockCnt *bl = fs->bl;
|
||||
while (bl->nactvar > level) bl = bl->previous;
|
||||
bl->upval = 1;
|
||||
|
@ -290,8 +267,7 @@ static void markupval(FuncState *fs, int level)
|
|||
Find variable with given name 'n'. If it is an upvalue, add this
|
||||
upvalue into all intermediate functions.
|
||||
*/
|
||||
static int singlevaraux(FuncState *fs, TString *n, expdesc *var, int base)
|
||||
{
|
||||
static int singlevaraux(FuncState *fs, TString *n, expdesc *var, int base) {
|
||||
if (fs == NULL) /* no more levels? */
|
||||
return VVOID; /* default is global */
|
||||
else {
|
||||
|
@ -316,8 +292,7 @@ static int singlevaraux(FuncState *fs, TString *n, expdesc *var, int base)
|
|||
}
|
||||
|
||||
|
||||
static void singlevar(LexState *ls, expdesc *var)
|
||||
{
|
||||
static void singlevar(LexState *ls, expdesc *var) {
|
||||
TString *varname = str_checkname(ls);
|
||||
FuncState *fs = ls->fs;
|
||||
if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */
|
||||
|
@ -330,8 +305,7 @@ static void singlevar(LexState *ls, expdesc *var)
|
|||
}
|
||||
|
||||
|
||||
static void adjust_assign(LexState *ls, int nvars, int nexps, expdesc *e)
|
||||
{
|
||||
static void adjust_assign(LexState *ls, int nvars, int nexps, expdesc *e) {
|
||||
FuncState *fs = ls->fs;
|
||||
int extra = nvars - nexps;
|
||||
if (hasmultret(e->k)) {
|
||||
|
@ -350,8 +324,7 @@ static void adjust_assign(LexState *ls, int nvars, int nexps, expdesc *e)
|
|||
}
|
||||
|
||||
|
||||
static void enterlevel(LexState *ls)
|
||||
{
|
||||
static void enterlevel(LexState *ls) {
|
||||
lua_State *L = ls->L;
|
||||
++L->nCcalls;
|
||||
checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels");
|
||||
|
@ -361,8 +334,7 @@ static void enterlevel(LexState *ls)
|
|||
#define leavelevel(ls) ((ls)->L->nCcalls--)
|
||||
|
||||
|
||||
static void closegoto(LexState *ls, int g, Labeldesc *label)
|
||||
{
|
||||
static void closegoto(LexState *ls, int g, Labeldesc *label) {
|
||||
int i;
|
||||
FuncState *fs = ls->fs;
|
||||
Labellist *gl = &ls->dyd->gt;
|
||||
|
@ -386,8 +358,7 @@ static void closegoto(LexState *ls, int g, Labeldesc *label)
|
|||
/*
|
||||
** try to close a goto with existing labels; this solves backward jumps
|
||||
*/
|
||||
static int findlabel(LexState *ls, int g)
|
||||
{
|
||||
static int findlabel(LexState *ls, int g) {
|
||||
int i;
|
||||
BlockCnt *bl = ls->fs->bl;
|
||||
Dyndata *dyd = ls->dyd;
|
||||
|
@ -397,7 +368,7 @@ static int findlabel(LexState *ls, int g)
|
|||
Labeldesc *lb = &dyd->label.arr[i];
|
||||
if (luaS_eqstr(lb->name, gt->name)) { /* correct label? */
|
||||
if (gt->nactvar > lb->nactvar &&
|
||||
(bl->upval || dyd->label.n > bl->firstlabel))
|
||||
(bl->upval || dyd->label.n > bl->firstlabel))
|
||||
luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
|
||||
closegoto(ls, g, lb); /* close it */
|
||||
return 1;
|
||||
|
@ -408,8 +379,7 @@ static int findlabel(LexState *ls, int g)
|
|||
|
||||
|
||||
static int newlabelentry(LexState *ls, Labellist *l, TString *name,
|
||||
int line, int pc)
|
||||
{
|
||||
int line, int pc) {
|
||||
int n = l->n;
|
||||
luaM_growvector(ls->L, l->arr, n, l->size,
|
||||
Labeldesc, SHRT_MAX, "labels/gotos");
|
||||
|
@ -426,8 +396,7 @@ static int newlabelentry(LexState *ls, Labellist *l, TString *name,
|
|||
** check whether new label 'lb' matches any pending gotos in current
|
||||
** block; solves forward jumps
|
||||
*/
|
||||
static void findgotos(LexState *ls, Labeldesc *lb)
|
||||
{
|
||||
static void findgotos(LexState *ls, Labeldesc *lb) {
|
||||
Labellist *gl = &ls->dyd->gt;
|
||||
int i = ls->fs->bl->firstgoto;
|
||||
while (i < gl->n) {
|
||||
|
@ -445,8 +414,7 @@ static void findgotos(LexState *ls, Labeldesc *lb)
|
|||
** the goto exits the scope of any variable (which can be the
|
||||
** upvalue), close those variables being exited.
|
||||
*/
|
||||
static void movegotosout(FuncState *fs, BlockCnt *bl)
|
||||
{
|
||||
static void movegotosout(FuncState *fs, BlockCnt *bl) {
|
||||
int i = bl->firstgoto;
|
||||
Labellist *gl = &fs->ls->dyd->gt;
|
||||
/* correct pending gotos to current block and try to close it
|
||||
|
@ -464,8 +432,7 @@ static void movegotosout(FuncState *fs, BlockCnt *bl)
|
|||
}
|
||||
|
||||
|
||||
static void enterblock(FuncState *fs, BlockCnt *bl, lu_byte isloop)
|
||||
{
|
||||
static void enterblock(FuncState *fs, BlockCnt *bl, lu_byte isloop) {
|
||||
bl->isloop = isloop;
|
||||
bl->nactvar = fs->nactvar;
|
||||
bl->firstlabel = fs->ls->dyd->label.n;
|
||||
|
@ -480,8 +447,7 @@ static void enterblock(FuncState *fs, BlockCnt *bl, lu_byte isloop)
|
|||
/*
|
||||
** create a label named "break" to resolve break statements
|
||||
*/
|
||||
static void breaklabel(LexState *ls)
|
||||
{
|
||||
static void breaklabel(LexState *ls) {
|
||||
TString *n = luaS_new(ls->L, "break");
|
||||
int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc);
|
||||
findgotos(ls, &ls->dyd->label.arr[l]);
|
||||
|
@ -491,8 +457,7 @@ static void breaklabel(LexState *ls)
|
|||
** generates an error for an undefined 'goto'; choose appropriate
|
||||
** message when label name is a reserved word (which can only be 'break')
|
||||
*/
|
||||
static l_noret undefgoto(LexState *ls, Labeldesc *gt)
|
||||
{
|
||||
static l_noret undefgoto(LexState *ls, Labeldesc *gt) {
|
||||
const char *msg = isreserved(gt->name)
|
||||
? "<%s> at line %d not inside a loop"
|
||||
: "no visible label " LUA_QS " for <goto> at line %d";
|
||||
|
@ -501,8 +466,7 @@ static l_noret undefgoto(LexState *ls, Labeldesc *gt)
|
|||
}
|
||||
|
||||
|
||||
static void leaveblock(FuncState *fs)
|
||||
{
|
||||
static void leaveblock(FuncState *fs) {
|
||||
BlockCnt *bl = fs->bl;
|
||||
LexState *ls = fs->ls;
|
||||
if (bl->previous && bl->upval) {
|
||||
|
@ -528,8 +492,7 @@ static void leaveblock(FuncState *fs)
|
|||
/*
|
||||
** adds a new prototype into list of prototypes
|
||||
*/
|
||||
static Proto *addprototype(LexState *ls)
|
||||
{
|
||||
static Proto *addprototype(LexState *ls) {
|
||||
Proto *clp;
|
||||
lua_State *L = ls->L;
|
||||
FuncState *fs = ls->fs;
|
||||
|
@ -551,16 +514,14 @@ static Proto *addprototype(LexState *ls)
|
|||
** so that, if it invokes the GC, the GC knows which registers
|
||||
** are in use at that time.
|
||||
*/
|
||||
static void codeclosure(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void codeclosure(LexState *ls, expdesc *v) {
|
||||
FuncState *fs = ls->fs->prev;
|
||||
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
|
||||
luaK_exp2nextreg(fs, v); /* fix it at the last register */
|
||||
}
|
||||
|
||||
|
||||
static void open_func(LexState *ls, FuncState *fs, BlockCnt *bl)
|
||||
{
|
||||
static void open_func(LexState *ls, FuncState *fs, BlockCnt *bl) {
|
||||
lua_State *L = ls->L;
|
||||
Proto *f;
|
||||
fs->prev = ls->fs; /* linked list of funcstates */
|
||||
|
@ -588,8 +549,7 @@ static void open_func(LexState *ls, FuncState *fs, BlockCnt *bl)
|
|||
}
|
||||
|
||||
|
||||
static void close_func(LexState *ls)
|
||||
{
|
||||
static void close_func(LexState *ls) {
|
||||
lua_State *L = ls->L;
|
||||
FuncState *fs = ls->fs;
|
||||
Proto *f = fs->f;
|
||||
|
@ -627,8 +587,7 @@ static void close_func(LexState *ls)
|
|||
** 'until' closes syntactical blocks, but do not close scope,
|
||||
** so it handled in separate.
|
||||
*/
|
||||
static int block_follow(LexState *ls, int withuntil)
|
||||
{
|
||||
static int block_follow(LexState *ls, int withuntil) {
|
||||
switch (ls->t.token) {
|
||||
case TK_ELSE:
|
||||
case TK_ELSEIF:
|
||||
|
@ -643,8 +602,7 @@ static int block_follow(LexState *ls, int withuntil)
|
|||
}
|
||||
|
||||
|
||||
static void statlist(LexState *ls)
|
||||
{
|
||||
static void statlist(LexState *ls) {
|
||||
/* statlist -> { stat [`;'] } */
|
||||
while (!block_follow(ls, 1)) {
|
||||
if (ls->t.token == TK_RETURN) {
|
||||
|
@ -656,8 +614,7 @@ static void statlist(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void fieldsel(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void fieldsel(LexState *ls, expdesc *v) {
|
||||
/* fieldsel -> ['.' | ':'] NAME */
|
||||
FuncState *fs = ls->fs;
|
||||
expdesc key;
|
||||
|
@ -668,8 +625,7 @@ static void fieldsel(LexState *ls, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static void yindex(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void yindex(LexState *ls, expdesc *v) {
|
||||
/* index -> '[' expr ']' */
|
||||
luaX_next(ls); /* skip the '[' */
|
||||
expr(ls, v);
|
||||
|
@ -694,8 +650,7 @@ struct ConsControl {
|
|||
};
|
||||
|
||||
|
||||
static void recfield(LexState *ls, struct ConsControl *cc)
|
||||
{
|
||||
static void recfield(LexState *ls, struct ConsControl *cc) {
|
||||
/* recfield -> (NAME | `['exp1`]') = exp1 */
|
||||
FuncState *fs = ls->fs;
|
||||
int reg = ls->fs->freereg;
|
||||
|
@ -715,8 +670,7 @@ static void recfield(LexState *ls, struct ConsControl *cc)
|
|||
}
|
||||
|
||||
|
||||
static void closelistfield(FuncState *fs, struct ConsControl *cc)
|
||||
{
|
||||
static void closelistfield(FuncState *fs, struct ConsControl *cc) {
|
||||
if (cc->v.k == VVOID) return; /* there is no list item */
|
||||
luaK_exp2nextreg(fs, &cc->v);
|
||||
cc->v.k = VVOID;
|
||||
|
@ -727,8 +681,7 @@ static void closelistfield(FuncState *fs, struct ConsControl *cc)
|
|||
}
|
||||
|
||||
|
||||
static void lastlistfield(FuncState *fs, struct ConsControl *cc)
|
||||
{
|
||||
static void lastlistfield(FuncState *fs, struct ConsControl *cc) {
|
||||
if (cc->tostore == 0) return;
|
||||
if (hasmultret(cc->v.k)) {
|
||||
luaK_setmultret(fs, &cc->v);
|
||||
|
@ -742,8 +695,7 @@ static void lastlistfield(FuncState *fs, struct ConsControl *cc)
|
|||
}
|
||||
|
||||
|
||||
static void listfield(LexState *ls, struct ConsControl *cc)
|
||||
{
|
||||
static void listfield(LexState *ls, struct ConsControl *cc) {
|
||||
/* listfield -> exp */
|
||||
expr(ls, &cc->v);
|
||||
checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
|
||||
|
@ -752,8 +704,7 @@ static void listfield(LexState *ls, struct ConsControl *cc)
|
|||
}
|
||||
|
||||
|
||||
static void field(LexState *ls, struct ConsControl *cc)
|
||||
{
|
||||
static void field(LexState *ls, struct ConsControl *cc) {
|
||||
/* field -> listfield | recfield */
|
||||
switch (ls->t.token) {
|
||||
case TK_NAME: { /* may be 'listfield' or 'recfield' */
|
||||
|
@ -775,8 +726,7 @@ static void field(LexState *ls, struct ConsControl *cc)
|
|||
}
|
||||
|
||||
|
||||
static void constructor(LexState *ls, expdesc *t)
|
||||
{
|
||||
static void constructor(LexState *ls, expdesc *t) {
|
||||
/* constructor -> '{' [ field { sep field } [sep] ] '}'
|
||||
sep -> ',' | ';' */
|
||||
FuncState *fs = ls->fs;
|
||||
|
@ -805,8 +755,7 @@ static void constructor(LexState *ls, expdesc *t)
|
|||
|
||||
|
||||
|
||||
static void parlist(LexState *ls)
|
||||
{
|
||||
static void parlist(LexState *ls) {
|
||||
/* parlist -> [ param { `,' param } ] */
|
||||
FuncState *fs = ls->fs;
|
||||
Proto *f = fs->f;
|
||||
|
@ -836,8 +785,7 @@ static void parlist(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void body(LexState *ls, expdesc *e, int ismethod, int line)
|
||||
{
|
||||
static void body(LexState *ls, expdesc *e, int ismethod, int line) {
|
||||
/* body -> `(' parlist `)' block END */
|
||||
FuncState new_fs;
|
||||
BlockCnt bl;
|
||||
|
@ -859,8 +807,7 @@ static void body(LexState *ls, expdesc *e, int ismethod, int line)
|
|||
}
|
||||
|
||||
|
||||
static int explist(LexState *ls, expdesc *v)
|
||||
{
|
||||
static int explist(LexState *ls, expdesc *v) {
|
||||
/* explist -> expr { `,' expr } */
|
||||
int n = 1; /* at least one expression */
|
||||
expr(ls, v);
|
||||
|
@ -873,8 +820,7 @@ static int explist(LexState *ls, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static void funcargs(LexState *ls, expdesc *f, int line)
|
||||
{
|
||||
static void funcargs(LexState *ls, expdesc *f, int line) {
|
||||
FuncState *fs = ls->fs;
|
||||
expdesc args;
|
||||
int base, nparams;
|
||||
|
@ -928,8 +874,7 @@ static void funcargs(LexState *ls, expdesc *f, int line)
|
|||
*/
|
||||
|
||||
|
||||
static void primaryexp(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void primaryexp(LexState *ls, expdesc *v) {
|
||||
/* primaryexp -> NAME | '(' expr ')' */
|
||||
switch (ls->t.token) {
|
||||
case '(': {
|
||||
|
@ -951,8 +896,7 @@ static void primaryexp(LexState *ls, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static void suffixedexp(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void suffixedexp(LexState *ls, expdesc *v) {
|
||||
/* suffixedexp ->
|
||||
primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
|
||||
FuncState *fs = ls->fs;
|
||||
|
@ -993,8 +937,7 @@ static void suffixedexp(LexState *ls, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static void simpleexp(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void simpleexp(LexState *ls, expdesc *v) {
|
||||
/* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... |
|
||||
constructor | FUNCTION body | suffixedexp */
|
||||
switch (ls->t.token) {
|
||||
|
@ -1044,8 +987,7 @@ static void simpleexp(LexState *ls, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static UnOpr getunopr(int op)
|
||||
{
|
||||
static UnOpr getunopr(int op) {
|
||||
switch (op) {
|
||||
case TK_NOT:
|
||||
return OPR_NOT;
|
||||
|
@ -1059,8 +1001,7 @@ static UnOpr getunopr(int op)
|
|||
}
|
||||
|
||||
|
||||
static BinOpr getbinopr(int op)
|
||||
{
|
||||
static BinOpr getbinopr(int op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return OPR_ADD;
|
||||
|
@ -1116,8 +1057,7 @@ static const struct {
|
|||
** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
|
||||
** where `binop' is any binary operator with a priority higher than `limit'
|
||||
*/
|
||||
static BinOpr subexpr(LexState *ls, expdesc *v, int limit)
|
||||
{
|
||||
static BinOpr subexpr(LexState *ls, expdesc *v, int limit) {
|
||||
BinOpr op;
|
||||
UnOpr uop;
|
||||
enterlevel(ls);
|
||||
|
@ -1146,8 +1086,7 @@ static BinOpr subexpr(LexState *ls, expdesc *v, int limit)
|
|||
}
|
||||
|
||||
|
||||
static void expr(LexState *ls, expdesc *v)
|
||||
{
|
||||
static void expr(LexState *ls, expdesc *v) {
|
||||
subexpr(ls, v, 0);
|
||||
}
|
||||
|
||||
|
@ -1162,8 +1101,7 @@ static void expr(LexState *ls, expdesc *v)
|
|||
*/
|
||||
|
||||
|
||||
static void block(LexState *ls)
|
||||
{
|
||||
static void block(LexState *ls) {
|
||||
/* block -> statlist */
|
||||
FuncState *fs = ls->fs;
|
||||
BlockCnt bl;
|
||||
|
@ -1189,8 +1127,7 @@ struct LHS_assign {
|
|||
** table. If so, save original upvalue/local value in a safe place and
|
||||
** use this safe copy in the previous assignment.
|
||||
*/
|
||||
static void check_conflict(LexState *ls, struct LHS_assign *lh, expdesc *v)
|
||||
{
|
||||
static void check_conflict(LexState *ls, struct LHS_assign *lh, expdesc *v) {
|
||||
FuncState *fs = ls->fs;
|
||||
int extra = fs->freereg; /* eventual position to save local variable */
|
||||
int conflict = 0;
|
||||
|
@ -1218,8 +1155,7 @@ static void check_conflict(LexState *ls, struct LHS_assign *lh, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static void assignment(LexState *ls, struct LHS_assign *lh, int nvars)
|
||||
{
|
||||
static void assignment(LexState *ls, struct LHS_assign *lh, int nvars) {
|
||||
expdesc e;
|
||||
check_condition(ls, vkisvar(lh->v.k), "syntax error");
|
||||
if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */
|
||||
|
@ -1250,8 +1186,7 @@ static void assignment(LexState *ls, struct LHS_assign *lh, int nvars)
|
|||
}
|
||||
|
||||
|
||||
static int cond(LexState *ls)
|
||||
{
|
||||
static int cond(LexState *ls) {
|
||||
/* cond -> exp */
|
||||
expdesc v;
|
||||
expr(ls, &v); /* read condition */
|
||||
|
@ -1261,8 +1196,7 @@ static int cond(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void gotostat(LexState *ls, int pc)
|
||||
{
|
||||
static void gotostat(LexState *ls, int pc) {
|
||||
int line = ls->linenumber;
|
||||
TString *label;
|
||||
int g;
|
||||
|
@ -1278,8 +1212,7 @@ static void gotostat(LexState *ls, int pc)
|
|||
|
||||
|
||||
/* check for repeated labels on the same block */
|
||||
static void checkrepeated(FuncState *fs, Labellist *ll, TString *label)
|
||||
{
|
||||
static void checkrepeated(FuncState *fs, Labellist *ll, TString *label) {
|
||||
int i;
|
||||
for (i = fs->bl->firstlabel; i < ll->n; i++) {
|
||||
if (luaS_eqstr(label, ll->arr[i].name)) {
|
||||
|
@ -1293,15 +1226,13 @@ static void checkrepeated(FuncState *fs, Labellist *ll, TString *label)
|
|||
|
||||
|
||||
/* skip no-op statements */
|
||||
static void skipnoopstat(LexState *ls)
|
||||
{
|
||||
static void skipnoopstat(LexState *ls) {
|
||||
while (ls->t.token == ';' || ls->t.token == TK_DBCOLON)
|
||||
statement(ls);
|
||||
}
|
||||
|
||||
|
||||
static void labelstat(LexState *ls, TString *label, int line)
|
||||
{
|
||||
static void labelstat(LexState *ls, TString *label, int line) {
|
||||
/* label -> '::' NAME '::' */
|
||||
FuncState *fs = ls->fs;
|
||||
Labellist *ll = &ls->dyd->label;
|
||||
|
@ -1319,8 +1250,7 @@ static void labelstat(LexState *ls, TString *label, int line)
|
|||
}
|
||||
|
||||
|
||||
static void whilestat(LexState *ls, int line)
|
||||
{
|
||||
static void whilestat(LexState *ls, int line) {
|
||||
/* whilestat -> WHILE cond DO block END */
|
||||
FuncState *fs = ls->fs;
|
||||
int whileinit;
|
||||
|
@ -1339,8 +1269,7 @@ static void whilestat(LexState *ls, int line)
|
|||
}
|
||||
|
||||
|
||||
static void repeatstat(LexState *ls, int line)
|
||||
{
|
||||
static void repeatstat(LexState *ls, int line) {
|
||||
/* repeatstat -> REPEAT block UNTIL cond */
|
||||
int condexit;
|
||||
FuncState *fs = ls->fs;
|
||||
|
@ -1360,8 +1289,7 @@ static void repeatstat(LexState *ls, int line)
|
|||
}
|
||||
|
||||
|
||||
static int exp1(LexState *ls)
|
||||
{
|
||||
static int exp1(LexState *ls) {
|
||||
expdesc e;
|
||||
int reg;
|
||||
expr(ls, &e);
|
||||
|
@ -1372,8 +1300,7 @@ static int exp1(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void forbody(LexState *ls, int base, int line, int nvars, int isnum)
|
||||
{
|
||||
static void forbody(LexState *ls, int base, int line, int nvars, int isnum) {
|
||||
/* forbody -> DO block */
|
||||
BlockCnt bl;
|
||||
FuncState *fs = ls->fs;
|
||||
|
@ -1399,8 +1326,7 @@ static void forbody(LexState *ls, int base, int line, int nvars, int isnum)
|
|||
}
|
||||
|
||||
|
||||
static void fornum(LexState *ls, TString *varname, int line)
|
||||
{
|
||||
static void fornum(LexState *ls, TString *varname, int line) {
|
||||
/* fornum -> NAME = exp1,exp1[,exp1] forbody */
|
||||
FuncState *fs = ls->fs;
|
||||
int base = fs->freereg;
|
||||
|
@ -1422,8 +1348,7 @@ static void fornum(LexState *ls, TString *varname, int line)
|
|||
}
|
||||
|
||||
|
||||
static void forlist(LexState *ls, TString *indexname)
|
||||
{
|
||||
static void forlist(LexState *ls, TString *indexname) {
|
||||
/* forlist -> NAME {,NAME} IN explist forbody */
|
||||
FuncState *fs = ls->fs;
|
||||
expdesc e;
|
||||
|
@ -1448,8 +1373,7 @@ static void forlist(LexState *ls, TString *indexname)
|
|||
}
|
||||
|
||||
|
||||
static void forstat(LexState *ls, int line)
|
||||
{
|
||||
static void forstat(LexState *ls, int line) {
|
||||
/* forstat -> FOR (fornum | forlist) END */
|
||||
FuncState *fs = ls->fs;
|
||||
TString *varname;
|
||||
|
@ -1473,8 +1397,7 @@ static void forstat(LexState *ls, int line)
|
|||
}
|
||||
|
||||
|
||||
static void test_then_block(LexState *ls, int *escapelist)
|
||||
{
|
||||
static void test_then_block(LexState *ls, int *escapelist) {
|
||||
/* test_then_block -> [IF | ELSEIF] cond THEN block */
|
||||
BlockCnt bl;
|
||||
FuncState *fs = ls->fs;
|
||||
|
@ -1501,14 +1424,13 @@ static void test_then_block(LexState *ls, int *escapelist)
|
|||
statlist(ls); /* `then' part */
|
||||
leaveblock(fs);
|
||||
if (ls->t.token == TK_ELSE ||
|
||||
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
||||
ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */
|
||||
luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */
|
||||
luaK_patchtohere(fs, jf);
|
||||
}
|
||||
|
||||
|
||||
static void ifstat(LexState *ls, int line)
|
||||
{
|
||||
static void ifstat(LexState *ls, int line) {
|
||||
/* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
|
||||
FuncState *fs = ls->fs;
|
||||
int escapelist = NO_JUMP; /* exit list for finished parts */
|
||||
|
@ -1522,8 +1444,7 @@ static void ifstat(LexState *ls, int line)
|
|||
}
|
||||
|
||||
|
||||
static void localfunc(LexState *ls)
|
||||
{
|
||||
static void localfunc(LexState *ls) {
|
||||
expdesc b;
|
||||
FuncState *fs = ls->fs;
|
||||
new_localvar(ls, str_checkname(ls)); /* new local variable */
|
||||
|
@ -1534,8 +1455,7 @@ static void localfunc(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void localstat(LexState *ls)
|
||||
{
|
||||
static void localstat(LexState *ls) {
|
||||
/* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
|
||||
int nvars = 0;
|
||||
int nexps;
|
||||
|
@ -1555,8 +1475,7 @@ static void localstat(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static int funcname(LexState *ls, expdesc *v)
|
||||
{
|
||||
static int funcname(LexState *ls, expdesc *v) {
|
||||
/* funcname -> NAME {fieldsel} [`:' NAME] */
|
||||
int ismethod = 0;
|
||||
singlevar(ls, v);
|
||||
|
@ -1570,8 +1489,7 @@ static int funcname(LexState *ls, expdesc *v)
|
|||
}
|
||||
|
||||
|
||||
static void funcstat(LexState *ls, int line)
|
||||
{
|
||||
static void funcstat(LexState *ls, int line) {
|
||||
/* funcstat -> FUNCTION funcname body */
|
||||
int ismethod;
|
||||
expdesc v, b;
|
||||
|
@ -1583,8 +1501,7 @@ static void funcstat(LexState *ls, int line)
|
|||
}
|
||||
|
||||
|
||||
static void exprstat(LexState *ls)
|
||||
{
|
||||
static void exprstat(LexState *ls) {
|
||||
/* stat -> func | assignment */
|
||||
FuncState *fs = ls->fs;
|
||||
struct LHS_assign v;
|
||||
|
@ -1599,8 +1516,7 @@ static void exprstat(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void retstat(LexState *ls)
|
||||
{
|
||||
static void retstat(LexState *ls) {
|
||||
/* stat -> RETURN [explist] [';'] */
|
||||
FuncState *fs = ls->fs;
|
||||
expdesc e;
|
||||
|
@ -1632,8 +1548,7 @@ static void retstat(LexState *ls)
|
|||
}
|
||||
|
||||
|
||||
static void statement(LexState *ls)
|
||||
{
|
||||
static void statement(LexState *ls) {
|
||||
int line = ls->linenumber; /* may be needed for error messages */
|
||||
enterlevel(ls);
|
||||
switch (ls->t.token) {
|
||||
|
@ -1708,8 +1623,7 @@ static void statement(LexState *ls)
|
|||
** compiles the main function, which is a regular vararg function with an
|
||||
** upvalue named LUA_ENV
|
||||
*/
|
||||
static void mainfunc(LexState *ls, FuncState *fs)
|
||||
{
|
||||
static void mainfunc(LexState *ls, FuncState *fs) {
|
||||
BlockCnt bl;
|
||||
expdesc v;
|
||||
open_func(ls, fs, &bl);
|
||||
|
@ -1724,8 +1638,7 @@ static void mainfunc(LexState *ls, FuncState *fs)
|
|||
|
||||
|
||||
Closure *luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff,
|
||||
Dyndata *dyd, const char *name, int firstchar)
|
||||
{
|
||||
Dyndata *dyd, const char *name, int firstchar) {
|
||||
LexState lexstate;
|
||||
FuncState funcstate;
|
||||
Closure *cl = luaF_newLclosure(L, 1); /* create main closure */
|
||||
|
|
|
@ -86,8 +86,7 @@ typedef struct LG {
|
|||
{ size_t t = cast(size_t, e); \
|
||||
memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
|
||||
|
||||
static unsigned int makeseed(lua_State *L)
|
||||
{
|
||||
static unsigned int makeseed(lua_State *L) {
|
||||
char buff[4 * sizeof(size_t)];
|
||||
unsigned int h = luai_makeseed();
|
||||
int p = 0;
|
||||
|
@ -104,15 +103,13 @@ static unsigned int makeseed(lua_State *L)
|
|||
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
||||
** invariant
|
||||
*/
|
||||
void luaE_setdebt(global_State *g, l_mem debt)
|
||||
{
|
||||
void luaE_setdebt(global_State *g, l_mem debt) {
|
||||
g->totalbytes -= (debt - g->GCdebt);
|
||||
g->GCdebt = debt;
|
||||
}
|
||||
|
||||
|
||||
CallInfo *luaE_extendCI(lua_State *L)
|
||||
{
|
||||
CallInfo *luaE_extendCI(lua_State *L) {
|
||||
CallInfo *ci = luaM_new(L, CallInfo);
|
||||
lua_assert(L->ci->next == NULL);
|
||||
L->ci->next = ci;
|
||||
|
@ -122,8 +119,7 @@ CallInfo *luaE_extendCI(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
void luaE_freeCI(lua_State *L)
|
||||
{
|
||||
void luaE_freeCI(lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
CallInfo *next = ci->next;
|
||||
ci->next = NULL;
|
||||
|
@ -134,8 +130,7 @@ void luaE_freeCI(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void stack_init(lua_State *L1, lua_State *L)
|
||||
{
|
||||
static void stack_init(lua_State *L1, lua_State *L) {
|
||||
int i;
|
||||
CallInfo *ci;
|
||||
/* initialize stack array */
|
||||
|
@ -156,8 +151,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 == NULL)
|
||||
return; /* stack not completely built yet */
|
||||
L->ci = &L->base_ci; /* free the entire 'ci' list */
|
||||
|
@ -169,8 +163,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) {
|
||||
TValue mt;
|
||||
/* create registry */
|
||||
Table *registry = luaH_new(L);
|
||||
|
@ -188,8 +181,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 */
|
||||
|
@ -208,8 +200,7 @@ static void f_luaopen(lua_State *L, void *ud)
|
|||
** preinitialize a state with consistent values without allocating
|
||||
** any memory (to avoid errors)
|
||||
*/
|
||||
static void preinit_state(lua_State *L, global_State *g)
|
||||
{
|
||||
static void preinit_state(lua_State *L, global_State *g) {
|
||||
G(L) = g;
|
||||
L->stack = NULL;
|
||||
L->ci = NULL;
|
||||
|
@ -228,8 +219,7 @@ static void preinit_state(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);
|
||||
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||
luaC_freeallobjects(L); /* collect all objects */
|
||||
|
@ -241,8 +231,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) {
|
||||
lua_State *L1;
|
||||
lua_lock(L);
|
||||
luaC_checkGC(L);
|
||||
|
@ -261,8 +250,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_close(L1, L1->stack); /* close all upvalues for this thread */
|
||||
lua_assert(L1->openupval == NULL);
|
||||
|
@ -272,8 +260,7 @@ void luaE_freethread(lua_State *L, lua_State *L1)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -325,8 +312,7 @@ 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) {
|
||||
L = G(L)->mainthread; /* only the main thread can be closed */
|
||||
lua_lock(L);
|
||||
luai_userstateclose(L);
|
||||
|
|
|
@ -30,8 +30,7 @@
|
|||
/*
|
||||
** equality for long strings
|
||||
*/
|
||||
int luaS_eqlngstr(TString *a, TString *b)
|
||||
{
|
||||
int luaS_eqlngstr(TString *a, TString *b) {
|
||||
size_t len = a->tsv.len;
|
||||
lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
|
||||
return (a == b) || /* same instance or... */
|
||||
|
@ -43,15 +42,13 @@ int luaS_eqlngstr(TString *a, TString *b)
|
|||
/*
|
||||
** equality for strings
|
||||
*/
|
||||
int luaS_eqstr(TString *a, TString *b)
|
||||
{
|
||||
int luaS_eqstr(TString *a, TString *b) {
|
||||
return (a->tsv.tt == b->tsv.tt) &&
|
||||
(a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, 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(unsigned int, l);
|
||||
size_t l1;
|
||||
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
||||
|
@ -64,8 +61,7 @@ unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
|
|||
/*
|
||||
** resizes the string table
|
||||
*/
|
||||
void luaS_resize(lua_State *L, int newsize)
|
||||
{
|
||||
void luaS_resize(lua_State *L, int newsize) {
|
||||
int i;
|
||||
stringtable *tb = &G(L)->strt;
|
||||
/* cannot resize while GC is traversing strings */
|
||||
|
@ -100,8 +96,7 @@ void luaS_resize(lua_State *L, int newsize)
|
|||
** creates a new string object
|
||||
*/
|
||||
static TString *createstrobj(lua_State *L, const char *str, size_t l,
|
||||
int tag, unsigned int h, GCObject **list)
|
||||
{
|
||||
int tag, unsigned int h, GCObject **list) {
|
||||
TString *ts;
|
||||
size_t totalsize; /* total size of TString object */
|
||||
totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
|
||||
|
@ -119,8 +114,7 @@ static TString *createstrobj(lua_State *L, const char *str, size_t l,
|
|||
** creates a new short string, inserting it into string table
|
||||
*/
|
||||
static TString *newshrstr(lua_State *L, const char *str, size_t l,
|
||||
unsigned int h)
|
||||
{
|
||||
unsigned int h) {
|
||||
GCObject **list; /* (pointer to) list where it will be inserted */
|
||||
stringtable *tb = &G(L)->strt;
|
||||
TString *s;
|
||||
|
@ -136,18 +130,17 @@ static TString *newshrstr(lua_State *L, const char *str, size_t l,
|
|||
/*
|
||||
** 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) {
|
||||
GCObject *o;
|
||||
global_State *g = G(L);
|
||||
unsigned int h = luaS_hash(str, l, g->seed);
|
||||
for (o = g->strt.hash[lmod(h, g->strt.size)];
|
||||
o != NULL;
|
||||
o = gch(o)->next) {
|
||||
o != NULL;
|
||||
o = gch(o)->next) {
|
||||
TString *ts = rawgco2ts(o);
|
||||
if (h == ts->tsv.hash &&
|
||||
l == ts->tsv.len &&
|
||||
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
||||
l == ts->tsv.len &&
|
||||
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
||||
if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
|
||||
changewhite(o); /* resurrect it */
|
||||
return ts;
|
||||
|
@ -160,8 +153,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 {
|
||||
|
@ -175,14 +167,12 @@ TString *luaS_newlstr(lua_State *L, const char *str, size_t l)
|
|||
/*
|
||||
** new zero-terminated string
|
||||
*/
|
||||
TString *luaS_new(lua_State *L, const char *str)
|
||||
{
|
||||
TString *luaS_new(lua_State *L, const char *str) {
|
||||
return luaS_newlstr(L, str, strlen(str));
|
||||
}
|
||||
|
||||
|
||||
Udata *luaS_newudata(lua_State *L, size_t s, Table *e)
|
||||
{
|
||||
Udata *luaS_newudata(lua_State *L, size_t s, Table *e) {
|
||||
Udata *u;
|
||||
if (s > MAX_SIZET - sizeof(Udata))
|
||||
luaM_toobig(L);
|
||||
|
|
134
liblua/lstrlib.c
134
liblua/lstrlib.c
|
@ -34,8 +34,7 @@
|
|||
|
||||
|
||||
|
||||
static int str_len(lua_State *L)
|
||||
{
|
||||
static int str_len(lua_State *L) {
|
||||
size_t l;
|
||||
luaL_checklstring(L, 1, &l);
|
||||
lua_pushinteger(L, (lua_Integer)l);
|
||||
|
@ -44,16 +43,14 @@ static int str_len(lua_State *L)
|
|||
|
||||
|
||||
/* translate a relative string position: negative means back from end */
|
||||
static size_t posrelat(ptrdiff_t pos, size_t len)
|
||||
{
|
||||
static size_t posrelat(ptrdiff_t pos, size_t len) {
|
||||
if (pos >= 0) return (size_t)pos;
|
||||
else if (0u - (size_t)pos > len) return 0;
|
||||
else return len - ((size_t) - pos) + 1;
|
||||
}
|
||||
|
||||
|
||||
static int str_sub(lua_State *L)
|
||||
{
|
||||
static int str_sub(lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
size_t start = posrelat(luaL_checkinteger(L, 2), l);
|
||||
|
@ -67,8 +64,7 @@ static int str_sub(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int str_reverse(lua_State *L)
|
||||
{
|
||||
static int str_reverse(lua_State *L) {
|
||||
size_t l, i;
|
||||
luaL_Buffer b;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
|
@ -80,8 +76,7 @@ static int str_reverse(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int str_lower(lua_State *L)
|
||||
{
|
||||
static int str_lower(lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
|
@ -94,8 +89,7 @@ static int str_lower(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int str_upper(lua_State *L)
|
||||
{
|
||||
static int str_upper(lua_State *L) {
|
||||
size_t l;
|
||||
size_t i;
|
||||
luaL_Buffer b;
|
||||
|
@ -111,8 +105,7 @@ static int str_upper(lua_State *L)
|
|||
/* reasonable limit to avoid arithmetic overflow */
|
||||
#define MAXSIZE ((~(size_t)0) >> 1)
|
||||
|
||||
static int str_rep(lua_State *L)
|
||||
{
|
||||
static int str_rep(lua_State *L) {
|
||||
size_t l, lsep;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
int n = luaL_checkint(L, 2);
|
||||
|
@ -139,8 +132,7 @@ static int str_rep(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int str_byte(lua_State *L)
|
||||
{
|
||||
static int str_byte(lua_State *L) {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, 1, &l);
|
||||
size_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
|
||||
|
@ -159,8 +151,7 @@ static int str_byte(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int str_char(lua_State *L)
|
||||
{
|
||||
static int str_char(lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of arguments */
|
||||
int i;
|
||||
luaL_Buffer b;
|
||||
|
@ -175,16 +166,14 @@ static int str_char(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int writer(lua_State *L, const void *b, size_t size, void *B)
|
||||
{
|
||||
static int writer(lua_State *L, const void *b, size_t size, void *B) {
|
||||
(void)L;
|
||||
luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int str_dump(lua_State *L)
|
||||
{
|
||||
static int str_dump(lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
lua_settop(L, 1);
|
||||
|
@ -236,8 +225,7 @@ static const char *match(MatchState *ms, const char *s, const char *p);
|
|||
#define SPECIALS "^$*+?.([%-"
|
||||
|
||||
|
||||
static int check_capture(MatchState *ms, int l)
|
||||
{
|
||||
static int check_capture(MatchState *ms, int l) {
|
||||
l -= '1';
|
||||
if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
|
||||
return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
|
||||
|
@ -245,8 +233,7 @@ static int check_capture(MatchState *ms, int l)
|
|||
}
|
||||
|
||||
|
||||
static int capture_to_close(MatchState *ms)
|
||||
{
|
||||
static int capture_to_close(MatchState *ms) {
|
||||
int level = ms->level;
|
||||
for (level--; level >= 0; level--)
|
||||
if (ms->capture[level].len == CAP_UNFINISHED) return level;
|
||||
|
@ -254,8 +241,7 @@ static int capture_to_close(MatchState *ms)
|
|||
}
|
||||
|
||||
|
||||
static const char *classend(MatchState *ms, const char *p)
|
||||
{
|
||||
static const char *classend(MatchState *ms, const char *p) {
|
||||
switch (*p++) {
|
||||
case L_ESC: {
|
||||
if (p == ms->p_end)
|
||||
|
@ -279,8 +265,7 @@ static const char *classend(MatchState *ms, const char *p)
|
|||
}
|
||||
|
||||
|
||||
static int match_class(int c, int cl)
|
||||
{
|
||||
static int match_class(int c, int cl) {
|
||||
int res;
|
||||
switch (tolower(cl)) {
|
||||
case 'a' :
|
||||
|
@ -323,8 +308,7 @@ static int match_class(int c, int cl)
|
|||
}
|
||||
|
||||
|
||||
static int matchbracketclass(int c, const char *p, const char *ec)
|
||||
{
|
||||
static int matchbracketclass(int c, const char *p, const char *ec) {
|
||||
int sig = 1;
|
||||
if (*(p + 1) == '^') {
|
||||
sig = 0;
|
||||
|
@ -346,8 +330,7 @@ static int matchbracketclass(int c, const char *p, const char *ec)
|
|||
|
||||
|
||||
static int singlematch(MatchState *ms, const char *s, const char *p,
|
||||
const char *ep)
|
||||
{
|
||||
const char *ep) {
|
||||
if (s >= ms->src_end)
|
||||
return 0;
|
||||
else {
|
||||
|
@ -367,8 +350,7 @@ static int singlematch(MatchState *ms, const char *s, const char *p,
|
|||
|
||||
|
||||
static const char *matchbalance(MatchState *ms, const char *s,
|
||||
const char *p)
|
||||
{
|
||||
const char *p) {
|
||||
if (p >= ms->p_end - 1)
|
||||
luaL_error(ms->L, "malformed pattern "
|
||||
"(missing arguments to " LUA_QL("%%b") ")");
|
||||
|
@ -388,8 +370,7 @@ static const char *matchbalance(MatchState *ms, const char *s,
|
|||
|
||||
|
||||
static const char *max_expand(MatchState *ms, const char *s,
|
||||
const char *p, const char *ep)
|
||||
{
|
||||
const char *p, const char *ep) {
|
||||
ptrdiff_t i = 0; /* counts maximum expand for item */
|
||||
while (singlematch(ms, s + i, p, ep))
|
||||
i++;
|
||||
|
@ -404,8 +385,7 @@ static const char *max_expand(MatchState *ms, const char *s,
|
|||
|
||||
|
||||
static const char *min_expand(MatchState *ms, const char *s,
|
||||
const char *p, const char *ep)
|
||||
{
|
||||
const char *p, const char *ep) {
|
||||
for (;;) {
|
||||
const char *res = match(ms, s, ep + 1);
|
||||
if (res != NULL)
|
||||
|
@ -418,8 +398,7 @@ static const char *min_expand(MatchState *ms, const char *s,
|
|||
|
||||
|
||||
static const char *start_capture(MatchState *ms, const char *s,
|
||||
const char *p, int what)
|
||||
{
|
||||
const char *p, int what) {
|
||||
const char *res;
|
||||
int level = ms->level;
|
||||
if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
|
||||
|
@ -433,8 +412,7 @@ static const char *start_capture(MatchState *ms, const char *s,
|
|||
|
||||
|
||||
static const char *end_capture(MatchState *ms, const char *s,
|
||||
const char *p)
|
||||
{
|
||||
const char *p) {
|
||||
int l = capture_to_close(ms);
|
||||
const char *res;
|
||||
ms->capture[l].len = s - ms->capture[l].init; /* close capture */
|
||||
|
@ -444,20 +422,18 @@ static const char *end_capture(MatchState *ms, const char *s,
|
|||
}
|
||||
|
||||
|
||||
static const char *match_capture(MatchState *ms, const char *s, int l)
|
||||
{
|
||||
static const char *match_capture(MatchState *ms, const char *s, int l) {
|
||||
size_t len;
|
||||
l = check_capture(ms, l);
|
||||
len = ms->capture[l].len;
|
||||
if ((size_t)(ms->src_end - s) >= len &&
|
||||
memcmp(ms->capture[l].init, s, len) == 0)
|
||||
memcmp(ms->capture[l].init, s, len) == 0)
|
||||
return s + len;
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *match(MatchState *ms, const char *s, const char *p)
|
||||
{
|
||||
static const char *match(MatchState *ms, const char *s, const char *p) {
|
||||
if (ms->matchdepth-- == 0)
|
||||
luaL_error(ms->L, "pattern too complex");
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
|
@ -500,7 +476,7 @@ init: /* using goto's to optimize tail recursion */
|
|||
ep = classend(ms, p); /* points to what is next */
|
||||
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
||||
if (!matchbracketclass(uchar(previous), p, ep - 1) &&
|
||||
matchbracketclass(uchar(*s), p, ep - 1)) {
|
||||
matchbracketclass(uchar(*s), p, ep - 1)) {
|
||||
p = ep;
|
||||
goto init; /* return match(ms, s, ep); */
|
||||
}
|
||||
|
@ -577,8 +553,7 @@ dflt: { /* pattern class plus optional suffix */
|
|||
|
||||
|
||||
static const char *lmemfind(const char *s1, size_t l1,
|
||||
const char *s2, size_t l2)
|
||||
{
|
||||
const char *s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
||||
else {
|
||||
|
@ -600,8 +575,7 @@ static const char *lmemfind(const char *s1, size_t l1,
|
|||
|
||||
|
||||
static void push_onecapture(MatchState *ms, int i, const char *s,
|
||||
const char *e)
|
||||
{
|
||||
const char *e) {
|
||||
if (i >= ms->level) {
|
||||
if (i == 0) /* ms->level == 0, too */
|
||||
lua_pushlstring(ms->L, s, e - s); /* add whole match */
|
||||
|
@ -618,8 +592,7 @@ static void push_onecapture(MatchState *ms, int i, const char *s,
|
|||
}
|
||||
|
||||
|
||||
static int push_captures(MatchState *ms, const char *s, const char *e)
|
||||
{
|
||||
static int push_captures(MatchState *ms, const char *s, const char *e) {
|
||||
int i;
|
||||
int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
|
||||
luaL_checkstack(ms->L, nlevels, "too many captures");
|
||||
|
@ -630,8 +603,7 @@ static int push_captures(MatchState *ms, const char *s, const char *e)
|
|||
|
||||
|
||||
/* check whether pattern has no special characters */
|
||||
static int nospecials(const char *p, size_t l)
|
||||
{
|
||||
static int nospecials(const char *p, size_t l) {
|
||||
size_t upto = 0;
|
||||
do {
|
||||
if (strpbrk(p + upto, SPECIALS))
|
||||
|
@ -642,8 +614,7 @@ static int nospecials(const char *p, size_t l)
|
|||
}
|
||||
|
||||
|
||||
static int str_find_aux(lua_State *L, int find)
|
||||
{
|
||||
static int str_find_aux(lua_State *L, int find) {
|
||||
size_t ls, lp;
|
||||
const char *s = luaL_checklstring(L, 1, &ls);
|
||||
const char *p = luaL_checklstring(L, 2, &lp);
|
||||
|
@ -694,20 +665,17 @@ static int str_find_aux(lua_State *L, int find)
|
|||
}
|
||||
|
||||
|
||||
static int str_find(lua_State *L)
|
||||
{
|
||||
static int str_find(lua_State *L) {
|
||||
return str_find_aux(L, 1);
|
||||
}
|
||||
|
||||
|
||||
static int str_match(lua_State *L)
|
||||
{
|
||||
static int str_match(lua_State *L) {
|
||||
return str_find_aux(L, 0);
|
||||
}
|
||||
|
||||
|
||||
static int gmatch_aux(lua_State *L)
|
||||
{
|
||||
static int gmatch_aux(lua_State *L) {
|
||||
MatchState ms;
|
||||
size_t ls, lp;
|
||||
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
|
||||
|
@ -719,8 +687,8 @@ static int gmatch_aux(lua_State *L)
|
|||
ms.src_end = s + ls;
|
||||
ms.p_end = p + lp;
|
||||
for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
|
||||
src <= ms.src_end;
|
||||
src++) {
|
||||
src <= ms.src_end;
|
||||
src++) {
|
||||
const char *e;
|
||||
ms.level = 0;
|
||||
lua_assert(ms.matchdepth == MAXCCALLS);
|
||||
|
@ -736,8 +704,7 @@ static int gmatch_aux(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int gmatch(lua_State *L)
|
||||
{
|
||||
static int gmatch(lua_State *L) {
|
||||
luaL_checkstring(L, 1);
|
||||
luaL_checkstring(L, 2);
|
||||
lua_settop(L, 2);
|
||||
|
@ -748,8 +715,7 @@ static int gmatch(lua_State *L)
|
|||
|
||||
|
||||
static void add_s(MatchState *ms, luaL_Buffer *b, const char *s,
|
||||
const char *e)
|
||||
{
|
||||
const char *e) {
|
||||
size_t l, i;
|
||||
const char *news = lua_tolstring(ms->L, 3, &l);
|
||||
for (i = 0; i < l; i++) {
|
||||
|
@ -774,8 +740,7 @@ static void add_s(MatchState *ms, luaL_Buffer *b, const char *s,
|
|||
|
||||
|
||||
static void add_value(MatchState *ms, luaL_Buffer *b, const char *s,
|
||||
const char *e, int tr)
|
||||
{
|
||||
const char *e, int tr) {
|
||||
lua_State *L = ms->L;
|
||||
switch (tr) {
|
||||
case LUA_TFUNCTION: {
|
||||
|
@ -804,8 +769,7 @@ static void add_value(MatchState *ms, luaL_Buffer *b, const char *s,
|
|||
}
|
||||
|
||||
|
||||
static int str_gsub(lua_State *L)
|
||||
{
|
||||
static int str_gsub(lua_State *L) {
|
||||
size_t srcl, lp;
|
||||
const char *src = luaL_checklstring(L, 1, &srcl);
|
||||
const char *p = luaL_checklstring(L, 2, &lp);
|
||||
|
@ -904,8 +868,7 @@ static int str_gsub(lua_State *L)
|
|||
#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
|
||||
|
||||
|
||||
static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
|
||||
{
|
||||
static void addquoted(lua_State *L, luaL_Buffer *b, int arg) {
|
||||
size_t l;
|
||||
const char *s = luaL_checklstring(L, arg, &l);
|
||||
luaL_addchar(b, '"');
|
||||
|
@ -927,8 +890,7 @@ static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
|
|||
luaL_addchar(b, '"');
|
||||
}
|
||||
|
||||
static const char *scanformat(lua_State *L, const char *strfrmt, char *form)
|
||||
{
|
||||
static const char *scanformat(lua_State *L, const char *strfrmt, char *form) {
|
||||
const char *p = strfrmt;
|
||||
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
|
||||
if ((size_t)(p - strfrmt) >= sizeof(FLAGS) / sizeof(char))
|
||||
|
@ -953,8 +915,7 @@ static const char *scanformat(lua_State *L, const char *strfrmt, char *form)
|
|||
/*
|
||||
** add length modifier into formats
|
||||
*/
|
||||
static void addlenmod(char *form, const char *lenmod)
|
||||
{
|
||||
static void addlenmod(char *form, const char *lenmod) {
|
||||
size_t l = strlen(form);
|
||||
size_t lm = strlen(lenmod);
|
||||
char spec = form[l - 1];
|
||||
|
@ -964,8 +925,7 @@ static void addlenmod(char *form, const char *lenmod)
|
|||
}
|
||||
|
||||
|
||||
static int str_format(lua_State *L)
|
||||
{
|
||||
static int str_format(lua_State *L) {
|
||||
int top = lua_gettop(L);
|
||||
int arg = 1;
|
||||
size_t sfl;
|
||||
|
@ -1079,8 +1039,7 @@ static const luaL_Reg strlib[] = {
|
|||
};
|
||||
|
||||
|
||||
static void createmetatable(lua_State *L)
|
||||
{
|
||||
static void createmetatable(lua_State *L) {
|
||||
lua_createtable(L, 0, 1); /* table to be metatable for strings */
|
||||
lua_pushliteral(L, ""); /* dummy string */
|
||||
lua_pushvalue(L, -2); /* copy table */
|
||||
|
@ -1095,8 +1054,7 @@ static void createmetatable(lua_State *L)
|
|||
/*
|
||||
** Open string library
|
||||
*/
|
||||
LUAMOD_API int luaopen_string(lua_State *L)
|
||||
{
|
||||
LUAMOD_API int luaopen_string(lua_State *L) {
|
||||
luaL_newlib(L, strlib);
|
||||
createmetatable(L);
|
||||
return 1;
|
||||
|
|
|
@ -77,8 +77,7 @@ static const Node dummynode_ = {
|
|||
/*
|
||||
** hash for lua_Numbers
|
||||
*/
|
||||
static Node *hashnum(const Table *t, lua_Number n)
|
||||
{
|
||||
static Node *hashnum(const Table *t, lua_Number n) {
|
||||
int i;
|
||||
luai_hashnum(i, n);
|
||||
if (i < 0) {
|
||||
|
@ -95,8 +94,7 @@ static Node *hashnum(const Table *t, lua_Number n)
|
|||
** returns the `main' position of an element in a table (that is, the index
|
||||
** of its hash value)
|
||||
*/
|
||||
static Node *mainposition(const Table *t, const TValue *key)
|
||||
{
|
||||
static Node *mainposition(const Table *t, const TValue *key) {
|
||||
switch (ttype(key)) {
|
||||
case LUA_TNUMBER:
|
||||
return hashnum(t, nvalue(key));
|
||||
|
@ -126,8 +124,7 @@ static Node *mainposition(const Table *t, const TValue *key)
|
|||
** returns the index for `key' if `key' is an appropriate key to live in
|
||||
** the array part of the table, -1 otherwise.
|
||||
*/
|
||||
static int arrayindex(const TValue *key)
|
||||
{
|
||||
static int arrayindex(const TValue *key) {
|
||||
if (ttisnumber(key)) {
|
||||
lua_Number n = nvalue(key);
|
||||
int k;
|
||||
|
@ -144,8 +141,7 @@ static int arrayindex(const TValue *key)
|
|||
** elements in the array part, then elements in the hash part. The
|
||||
** beginning of a traversal is signaled by -1.
|
||||
*/
|
||||
static int findindex(lua_State *L, Table *t, StkId key)
|
||||
{
|
||||
static int findindex(lua_State *L, Table *t, StkId key) {
|
||||
int i;
|
||||
if (ttisnil(key)) return -1; /* first iteration */
|
||||
i = arrayindex(key);
|
||||
|
@ -156,8 +152,8 @@ static int findindex(lua_State *L, Table *t, StkId key)
|
|||
for (;;) { /* check whether `key' is somewhere in the chain */
|
||||
/* key may be dead already, but it is ok to use it in `next' */
|
||||
if (luaV_rawequalobj(gkey(n), key) ||
|
||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||
deadvalue(gkey(n)) == gcvalue(key))) {
|
||||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
|
||||
deadvalue(gkey(n)) == gcvalue(key))) {
|
||||
i = cast_int(n - gnode(t, 0)); /* key index in hash table */
|
||||
/* hash elements are numbered after array ones */
|
||||
return i + t->sizearray;
|
||||
|
@ -169,8 +165,7 @@ static int findindex(lua_State *L, Table *t, StkId key)
|
|||
}
|
||||
|
||||
|
||||
int luaH_next(lua_State *L, Table *t, StkId key)
|
||||
{
|
||||
int luaH_next(lua_State *L, Table *t, StkId key) {
|
||||
int i = findindex(L, t, key); /* find original element */
|
||||
for (i++; i < t->sizearray; i++) { /* try first array part */
|
||||
if (!ttisnil(&t->array[i])) { /* a non-nil value? */
|
||||
|
@ -197,8 +192,7 @@ int luaH_next(lua_State *L, Table *t, StkId key)
|
|||
*/
|
||||
|
||||
|
||||
static int computesizes(int nums[], int *narray)
|
||||
{
|
||||
static int computesizes(int nums[], int *narray) {
|
||||
int i;
|
||||
int twotoi; /* 2^i */
|
||||
int a = 0; /* number of elements smaller than 2^i */
|
||||
|
@ -220,8 +214,7 @@ static int computesizes(int nums[], int *narray)
|
|||
}
|
||||
|
||||
|
||||
static int countint(const TValue *key, int *nums)
|
||||
{
|
||||
static int countint(const TValue *key, int *nums) {
|
||||
int k = arrayindex(key);
|
||||
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
|
||||
nums[luaO_ceillog2(k)]++; /* count as such */
|
||||
|
@ -231,8 +224,7 @@ static int countint(const TValue *key, int *nums)
|
|||
}
|
||||
|
||||
|
||||
static int numusearray(const Table *t, int *nums)
|
||||
{
|
||||
static int numusearray(const Table *t, int *nums) {
|
||||
int lg;
|
||||
int ttlg; /* 2^lg */
|
||||
int ause = 0; /* summation of `nums' */
|
||||
|
@ -257,8 +249,7 @@ static int numusearray(const Table *t, int *nums)
|
|||
}
|
||||
|
||||
|
||||
static int numusehash(const Table *t, int *nums, int *pnasize)
|
||||
{
|
||||
static int numusehash(const Table *t, int *nums, int *pnasize) {
|
||||
int totaluse = 0; /* total number of elements */
|
||||
int ause = 0; /* summation of `nums' */
|
||||
int i = sizenode(t);
|
||||
|
@ -274,8 +265,7 @@ static int numusehash(const Table *t, int *nums, int *pnasize)
|
|||
}
|
||||
|
||||
|
||||
static void setarrayvector(lua_State *L, Table *t, int size)
|
||||
{
|
||||
static void setarrayvector(lua_State *L, Table *t, int size) {
|
||||
int i;
|
||||
luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
|
||||
for (i = t->sizearray; i < size; i++)
|
||||
|
@ -284,8 +274,7 @@ static void setarrayvector(lua_State *L, Table *t, int size)
|
|||
}
|
||||
|
||||
|
||||
static void setnodevector(lua_State *L, Table *t, int size)
|
||||
{
|
||||
static void setnodevector(lua_State *L, Table *t, int size) {
|
||||
int lsize;
|
||||
if (size == 0) { /* no elements to hash part? */
|
||||
t->node = cast(Node *, dummynode); /* use common `dummynode' */
|
||||
|
@ -309,8 +298,7 @@ static void setnodevector(lua_State *L, Table *t, int size)
|
|||
}
|
||||
|
||||
|
||||
void luaH_resize(lua_State *L, Table *t, int nasize, int nhsize)
|
||||
{
|
||||
void luaH_resize(lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
int i;
|
||||
int oldasize = t->sizearray;
|
||||
int oldhsize = t->lsizenode;
|
||||
|
@ -343,15 +331,13 @@ void luaH_resize(lua_State *L, Table *t, int nasize, int nhsize)
|
|||
}
|
||||
|
||||
|
||||
void luaH_resizearray(lua_State *L, Table *t, int nasize)
|
||||
{
|
||||
void luaH_resizearray(lua_State *L, Table *t, int nasize) {
|
||||
int nsize = isdummy(t->node) ? 0 : sizenode(t);
|
||||
luaH_resize(L, t, nasize, nsize);
|
||||
}
|
||||
|
||||
|
||||
static void rehash(lua_State *L, Table *t, const TValue *ek)
|
||||
{
|
||||
static void rehash(lua_State *L, Table *t, const TValue *ek) {
|
||||
int nasize, na;
|
||||
int nums[MAXBITS + 1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
|
||||
int i;
|
||||
|
@ -376,8 +362,7 @@ static void rehash(lua_State *L, Table *t, const TValue *ek)
|
|||
*/
|
||||
|
||||
|
||||
Table *luaH_new(lua_State *L)
|
||||
{
|
||||
Table *luaH_new(lua_State *L) {
|
||||
Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
|
||||
t->metatable = NULL;
|
||||
t->flags = cast_byte(~0);
|
||||
|
@ -388,8 +373,7 @@ Table *luaH_new(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
void luaH_free(lua_State *L, Table *t)
|
||||
{
|
||||
void luaH_free(lua_State *L, Table *t) {
|
||||
if (!isdummy(t->node))
|
||||
luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
|
||||
luaM_freearray(L, t->array, t->sizearray);
|
||||
|
@ -397,8 +381,7 @@ void luaH_free(lua_State *L, Table *t)
|
|||
}
|
||||
|
||||
|
||||
static Node *getfreepos(Table *t)
|
||||
{
|
||||
static Node *getfreepos(Table *t) {
|
||||
while (t->lastfree > t->node) {
|
||||
t->lastfree--;
|
||||
if (ttisnil(gkey(t->lastfree)))
|
||||
|
@ -416,8 +399,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.
|
||||
*/
|
||||
TValue *luaH_newkey(lua_State *L, Table *t, const TValue *key)
|
||||
{
|
||||
TValue *luaH_newkey(lua_State *L, Table *t, const TValue *key) {
|
||||
Node *mp;
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
|
||||
|
@ -457,8 +439,7 @@ TValue *luaH_newkey(lua_State *L, Table *t, const TValue *key)
|
|||
/*
|
||||
** search function for integers
|
||||
*/
|
||||
const TValue *luaH_getint(Table *t, int key)
|
||||
{
|
||||
const TValue *luaH_getint(Table *t, int key) {
|
||||
/* (1 <= key && key <= t->sizearray) */
|
||||
if (cast(unsigned int, key - 1) < cast(unsigned int, t->sizearray))
|
||||
return &t->array[key - 1];
|
||||
|
@ -478,8 +459,7 @@ const TValue *luaH_getint(Table *t, int key)
|
|||
/*
|
||||
** search function for short strings
|
||||
*/
|
||||
const TValue *luaH_getstr(Table *t, TString *key)
|
||||
{
|
||||
const TValue *luaH_getstr(Table *t, TString *key) {
|
||||
Node *n = hashstr(t, key);
|
||||
lua_assert(key->tsv.tt == LUA_TSHRSTR);
|
||||
do { /* check whether `key' is somewhere in the chain */
|
||||
|
@ -494,8 +474,7 @@ 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 (ttype(key)) {
|
||||
case LUA_TSHRSTR:
|
||||
return luaH_getstr(t, rawtsvalue(key));
|
||||
|
@ -526,8 +505,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.
|
||||
*/
|
||||
TValue *luaH_set(lua_State *L, Table *t, const TValue *key)
|
||||
{
|
||||
TValue *luaH_set(lua_State *L, Table *t, const TValue *key) {
|
||||
const TValue *p = luaH_get(t, key);
|
||||
if (p != luaO_nilobject)
|
||||
return cast(TValue *, p);
|
||||
|
@ -535,8 +513,7 @@ TValue *luaH_set(lua_State *L, Table *t, const TValue *key)
|
|||
}
|
||||
|
||||
|
||||
void luaH_setint(lua_State *L, Table *t, int key, TValue *value)
|
||||
{
|
||||
void luaH_setint(lua_State *L, Table *t, int key, TValue *value) {
|
||||
const TValue *p = luaH_getint(t, key);
|
||||
TValue *cell;
|
||||
if (p != luaO_nilobject)
|
||||
|
@ -550,8 +527,7 @@ void luaH_setint(lua_State *L, Table *t, int key, TValue *value)
|
|||
}
|
||||
|
||||
|
||||
static int unbound_search(Table *t, unsigned int j)
|
||||
{
|
||||
static int unbound_search(Table *t, unsigned int j) {
|
||||
unsigned int i = j; /* i is zero or a present index */
|
||||
j++;
|
||||
/* find `i' and `j' such that i is present and j is not */
|
||||
|
@ -579,8 +555,7 @@ static int unbound_search(Table *t, unsigned int j)
|
|||
** Try to find a boundary in table `t'. A `boundary' is an integer index
|
||||
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
|
||||
*/
|
||||
int luaH_getn(Table *t)
|
||||
{
|
||||
int luaH_getn(Table *t) {
|
||||
unsigned int j = t->sizearray;
|
||||
if (j > 0 && ttisnil(&t->array[j - 1])) {
|
||||
/* there is a boundary in the array part: (binary) search for it */
|
||||
|
@ -602,8 +577,7 @@ int luaH_getn(Table *t)
|
|||
|
||||
#if defined(LUA_DEBUG)
|
||||
|
||||
Node *luaH_mainposition(const Table *t, const TValue *key)
|
||||
{
|
||||
Node *luaH_mainposition(const Table *t, const TValue *key) {
|
||||
return mainposition(t, key);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,7 @@
|
|||
|
||||
|
||||
#if defined(LUA_COMPAT_MAXN)
|
||||
static int maxn(lua_State *L)
|
||||
{
|
||||
static int maxn(lua_State *L) {
|
||||
lua_Number max = 0;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
lua_pushnil(L); /* first key */
|
||||
|
@ -39,8 +38,7 @@ static int maxn(lua_State *L)
|
|||
#endif
|
||||
|
||||
|
||||
static int tinsert(lua_State *L)
|
||||
{
|
||||
static int tinsert(lua_State *L) {
|
||||
int e = aux_getn(L, 1) + 1; /* first empty element */
|
||||
int pos; /* where to insert new element */
|
||||
switch (lua_gettop(L)) {
|
||||
|
@ -67,8 +65,7 @@ static int tinsert(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int tremove(lua_State *L)
|
||||
{
|
||||
static int tremove(lua_State *L) {
|
||||
int size = aux_getn(L, 1);
|
||||
int pos = luaL_optint(L, 2, size);
|
||||
if (pos != size) /* validate 'pos' if given */
|
||||
|
@ -84,8 +81,7 @@ static int tremove(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void addfield(lua_State *L, luaL_Buffer *b, int i)
|
||||
{
|
||||
static void addfield(lua_State *L, luaL_Buffer *b, int i) {
|
||||
lua_rawgeti(L, 1, i);
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_error(L, "invalid value (%s) at index %d in table for "
|
||||
|
@ -94,8 +90,7 @@ static void addfield(lua_State *L, luaL_Buffer *b, int i)
|
|||
}
|
||||
|
||||
|
||||
static int tconcat(lua_State *L)
|
||||
{
|
||||
static int tconcat(lua_State *L) {
|
||||
luaL_Buffer b;
|
||||
size_t lsep;
|
||||
int i, last;
|
||||
|
@ -121,8 +116,7 @@ static int tconcat(lua_State *L)
|
|||
** =======================================================
|
||||
*/
|
||||
|
||||
static int pack(lua_State *L)
|
||||
{
|
||||
static int pack(lua_State *L) {
|
||||
int n = lua_gettop(L); /* number of elements to pack */
|
||||
lua_createtable(L, n, 1); /* create result table */
|
||||
lua_pushinteger(L, n);
|
||||
|
@ -139,8 +133,7 @@ static int pack(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int unpack(lua_State *L)
|
||||
{
|
||||
static int unpack(lua_State *L) {
|
||||
int i, e, n;
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
i = luaL_optint(L, 2, 1);
|
||||
|
@ -168,14 +161,12 @@ static int unpack(lua_State *L)
|
|||
*/
|
||||
|
||||
|
||||
static void set2(lua_State *L, int i, int j)
|
||||
{
|
||||
static void set2(lua_State *L, int i, int j) {
|
||||
lua_rawseti(L, 1, i);
|
||||
lua_rawseti(L, 1, j);
|
||||
}
|
||||
|
||||
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)) { /* function? */
|
||||
int res;
|
||||
lua_pushvalue(L, 2);
|
||||
|
@ -189,8 +180,7 @@ static int sort_comp(lua_State *L, int a, int b)
|
|||
return lua_compare(L, a, b, LUA_OPLT);
|
||||
}
|
||||
|
||||
static void auxsort(lua_State *L, int l, int u)
|
||||
{
|
||||
static void auxsort(lua_State *L, int l, int u) {
|
||||
while (l < u) { /* for tail recursion */
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
|
@ -257,8 +247,7 @@ static void auxsort(lua_State *L, int l, int u)
|
|||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
static int sort(lua_State *L)
|
||||
{
|
||||
static int sort(lua_State *L) {
|
||||
int n = aux_getn(L, 1);
|
||||
luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
|
||||
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
|
||||
|
@ -285,8 +274,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);
|
||||
#if defined(LUA_COMPAT_UNPACK)
|
||||
/* _G.unpack = table.unpack */
|
||||
|
|
|
@ -29,8 +29,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
|||
};
|
||||
|
||||
|
||||
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",
|
||||
|
@ -50,8 +49,7 @@ 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_getstr(events, ename);
|
||||
lua_assert(event <= TM_EQ);
|
||||
if (ttisnil(tm)) { /* no tag method? */
|
||||
|
@ -61,8 +59,7 @@ const TValue *luaT_gettm(Table *events, TMS event, TString *ename)
|
|||
}
|
||||
|
||||
|
||||
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 (ttypenv(o)) {
|
||||
case LUA_TTABLE:
|
||||
|
|
72
liblua/lua.c
72
liblua/lua.c
|
@ -91,24 +91,21 @@ static const char *progname = LUA_PROGNAME;
|
|||
|
||||
|
||||
|
||||
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);
|
||||
luaL_error(L, "interrupted!");
|
||||
}
|
||||
|
||||
|
||||
static void laction(int i)
|
||||
{
|
||||
static void laction(int i) {
|
||||
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
terminate process (default action) */
|
||||
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
||||
}
|
||||
|
||||
|
||||
static void print_usage(const char *badoption)
|
||||
{
|
||||
static void print_usage(const char *badoption) {
|
||||
luai_writestringerror("%s: ", progname);
|
||||
if (badoption[1] == 'e' || badoption[1] == 'l')
|
||||
luai_writestringerror("'%s' needs argument\n", badoption);
|
||||
|
@ -129,15 +126,13 @@ static void print_usage(const char *badoption)
|
|||
}
|
||||
|
||||
|
||||
static void l_message(const char *pname, const char *msg)
|
||||
{
|
||||
static void l_message(const char *pname, const char *msg) {
|
||||
if (pname) luai_writestringerror("%s: ", pname);
|
||||
luai_writestringerror("%s\n", msg);
|
||||
}
|
||||
|
||||
|
||||
static int report(lua_State *L, int status)
|
||||
{
|
||||
static int report(lua_State *L, int status) {
|
||||
if (status != LUA_OK && !lua_isnil(L, -1)) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "(error object is not a string)";
|
||||
|
@ -151,8 +146,7 @@ static int report(lua_State *L, int status)
|
|||
|
||||
|
||||
/* the next function is called unprotected, so it must avoid errors */
|
||||
static void finalreport(lua_State *L, int status)
|
||||
{
|
||||
static void finalreport(lua_State *L, int status) {
|
||||
if (status != LUA_OK) {
|
||||
const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1)
|
||||
: NULL;
|
||||
|
@ -163,8 +157,7 @@ static void finalreport(lua_State *L, int status)
|
|||
}
|
||||
|
||||
|
||||
static int traceback(lua_State *L)
|
||||
{
|
||||
static int traceback(lua_State *L) {
|
||||
const char *msg = lua_tostring(L, 1);
|
||||
if (msg)
|
||||
luaL_traceback(L, L, msg, 1);
|
||||
|
@ -176,8 +169,7 @@ static int traceback(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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, traceback); /* push traceback function */
|
||||
|
@ -191,15 +183,13 @@ static int docall(lua_State *L, int narg, int nres)
|
|||
}
|
||||
|
||||
|
||||
static void print_version(void)
|
||||
{
|
||||
static void print_version(void) {
|
||||
luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
|
||||
luai_writeline();
|
||||
}
|
||||
|
||||
|
||||
static int getargs(lua_State *L, char **argv, int n)
|
||||
{
|
||||
static int getargs(lua_State *L, char **argv, int n) {
|
||||
int narg;
|
||||
int i;
|
||||
int argc = 0;
|
||||
|
@ -217,24 +207,21 @@ static int getargs(lua_State *L, char **argv, int n)
|
|||
}
|
||||
|
||||
|
||||
static int dofile(lua_State *L, const char *name)
|
||||
{
|
||||
static int dofile(lua_State *L, const char *name) {
|
||||
int status = luaL_loadfile(L, name);
|
||||
if (status == LUA_OK) status = docall(L, 0, 0);
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int dostring(lua_State *L, const char *s, const char *name)
|
||||
{
|
||||
static int dostring(lua_State *L, const char *s, const char *name) {
|
||||
int status = luaL_loadbuffer(L, s, strlen(s), name);
|
||||
if (status == LUA_OK) status = docall(L, 0, 0);
|
||||
return report(L, status);
|
||||
}
|
||||
|
||||
|
||||
static int dolibrary(lua_State *L, const char *name)
|
||||
{
|
||||
static int dolibrary(lua_State *L, const char *name) {
|
||||
int status;
|
||||
lua_getglobal(L, "require");
|
||||
lua_pushstring(L, name);
|
||||
|
@ -245,8 +232,7 @@ static int dolibrary(lua_State *L, const char *name)
|
|||
}
|
||||
|
||||
|
||||
static const char *get_prompt(lua_State *L, int firstline)
|
||||
{
|
||||
static const char *get_prompt(lua_State *L, int firstline) {
|
||||
const char *p;
|
||||
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
|
||||
p = lua_tostring(L, -1);
|
||||
|
@ -258,8 +244,7 @@ static const char *get_prompt(lua_State *L, int firstline)
|
|||
#define EOFMARK "<eof>"
|
||||
#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
|
||||
|
||||
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);
|
||||
|
@ -272,8 +257,7 @@ static int incomplete(lua_State *L, int status)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -294,8 +278,7 @@ static int pushline(lua_State *L, int firstline)
|
|||
}
|
||||
|
||||
|
||||
static int loadline(lua_State *L)
|
||||
{
|
||||
static int loadline(lua_State *L) {
|
||||
int status;
|
||||
lua_settop(L, 0);
|
||||
if (!pushline(L, 1))
|
||||
|
@ -317,8 +300,7 @@ static int loadline(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static void dotty(lua_State *L)
|
||||
{
|
||||
static void dotty(lua_State *L) {
|
||||
int status;
|
||||
const char *oldprogname = progname;
|
||||
progname = NULL;
|
||||
|
@ -341,8 +323,7 @@ static void dotty(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
static int handle_script(lua_State *L, char **argv, int n)
|
||||
{
|
||||
static int handle_script(lua_State *L, char **argv, int n) {
|
||||
int status;
|
||||
const char *fname;
|
||||
int narg = getargs(L, argv, n); /* collect arguments */
|
||||
|
@ -373,8 +354,7 @@ static int handle_script(lua_State *L, char **argv, int n)
|
|||
#define num_has 4 /* number of 'has_*' */
|
||||
|
||||
|
||||
static int collectargs(char **argv, int *args)
|
||||
{
|
||||
static int collectargs(char **argv, int *args) {
|
||||
int i;
|
||||
for (i = 1; argv[i] != NULL; i++) {
|
||||
if (argv[i][0] != '-') /* not an option? */
|
||||
|
@ -412,8 +392,7 @@ static int collectargs(char **argv, int *args)
|
|||
}
|
||||
|
||||
|
||||
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++) {
|
||||
lua_assert(argv[i][0] == '-');
|
||||
|
@ -442,8 +421,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_INITVERSION;
|
||||
const char *init = getenv(name + 1);
|
||||
if (init == NULL) {
|
||||
|
@ -458,8 +436,7 @@ static int handle_luainit(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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;
|
||||
|
@ -500,8 +477,7 @@ static int pmain(lua_State *L)
|
|||
}
|
||||
|
||||
|
||||
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) {
|
||||
|
|
|
@ -32,20 +32,17 @@ 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 void fatal(const char *message)
|
||||
{
|
||||
static void fatal(const char *message) {
|
||||
fprintf(stderr, "%s: %s\n", progname, message);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static void cannot(const char *what)
|
||||
{
|
||||
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)
|
||||
{
|
||||
static void usage(const char *message) {
|
||||
if (*message == '-')
|
||||
fprintf(stderr, "%s: unrecognized option " LUA_QS "\n", progname, message);
|
||||
else
|
||||
|
@ -66,8 +63,7 @@ static void usage(const char *message)
|
|||
|
||||
#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];
|
||||
|
@ -109,8 +105,7 @@ static int doargs(int argc, char *argv[])
|
|||
|
||||
#define FUNCTION "(function()end)();"
|
||||
|
||||
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;
|
||||
|
@ -123,8 +118,7 @@ static const char *reader(lua_State *L, void *ud, size_t *size)
|
|||
|
||||
#define toproto(L,i) getproto(L->top+(i))
|
||||
|
||||
static const Proto *combine(lua_State *L, int n)
|
||||
{
|
||||
static const Proto *combine(lua_State *L, int n) {
|
||||
if (n == 1)
|
||||
return toproto(L, -1);
|
||||
else {
|
||||
|
@ -141,14 +135,12 @@ static const Proto *combine(lua_State *L, int n)
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
const Proto *f;
|
||||
|
@ -172,8 +164,7 @@ static int pmain(lua_State *L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
lua_State *L;
|
||||
int i = doargs(argc, argv);
|
||||
argc -= i;
|
||||
|
@ -207,8 +198,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
#define VOID(p) ((const void*)(p))
|
||||
|
||||
static void PrintString(const TString *ts)
|
||||
{
|
||||
static void PrintString(const TString *ts) {
|
||||
const char *s = getstr(ts);
|
||||
size_t i, n = ts->tsv.len;
|
||||
printf("%c", '"');
|
||||
|
@ -252,8 +242,7 @@ static void PrintString(const TString *ts)
|
|||
printf("%c", '"');
|
||||
}
|
||||
|
||||
static void PrintConstant(const Proto *f, int i)
|
||||
{
|
||||
static void PrintConstant(const Proto *f, int i) {
|
||||
const TValue *o = &f->k[i];
|
||||
switch (ttype(o)) {
|
||||
case LUA_TNIL:
|
||||
|
@ -277,8 +266,7 @@ static void PrintConstant(const Proto *f, int i)
|
|||
#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
|
||||
#define MYK(x) (-1-(x))
|
||||
|
||||
static void PrintCode(const Proto *f)
|
||||
{
|
||||
static void PrintCode(const Proto *f) {
|
||||
const Instruction *code = f->code;
|
||||
int pc, n = f->sizecode;
|
||||
for (pc = 0; pc < n; pc++) {
|
||||
|
@ -380,8 +368,7 @@ 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)
|
||||
{
|
||||
static void PrintHeader(const Proto *f) {
|
||||
const char *s = f->source ? getstr(f->source) : "=?";
|
||||
if (*s == '@' || *s == '=')
|
||||
s++;
|
||||
|
@ -400,8 +387,7 @@ static void PrintHeader(const Proto *f)
|
|||
S(f->sizelocvars), S(f->sizek), S(f->sizep));
|
||||
}
|
||||
|
||||
static void PrintDebug(const Proto *f)
|
||||
{
|
||||
static void PrintDebug(const Proto *f) {
|
||||
int i, n;
|
||||
n = f->sizek;
|
||||
printf("constants (%d) for %p:\n", n, VOID(f));
|
||||
|
@ -424,8 +410,7 @@ static void PrintDebug(const Proto *f)
|
|||
}
|
||||
}
|
||||
|
||||
static void PrintFunction(const Proto *f, int full)
|
||||
{
|
||||
static void PrintFunction(const Proto *f, int full) {
|
||||
int i, n = f->sizep;
|
||||
PrintHeader(f);
|
||||
PrintCode(f);
|
||||
|
|
|
@ -27,8 +27,7 @@ typedef struct {
|
|||
const char *name;
|
||||
} LoadState;
|
||||
|
||||
static l_noret error(LoadState *S, const char *why)
|
||||
{
|
||||
static l_noret error(LoadState *S, const char *why) {
|
||||
luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
|
||||
luaD_throw(S->L, LUA_ERRSYNTAX);
|
||||
}
|
||||
|
@ -42,35 +41,30 @@ static l_noret error(LoadState *S, const char *why)
|
|||
#define luai_verifycode(L,b,f) /* empty */
|
||||
#endif
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
static int LoadChar(LoadState *S)
|
||||
{
|
||||
static int LoadChar(LoadState *S) {
|
||||
char x;
|
||||
LoadVar(S, x);
|
||||
return x;
|
||||
}
|
||||
|
||||
static int LoadInt(LoadState *S)
|
||||
{
|
||||
static int LoadInt(LoadState *S) {
|
||||
int x;
|
||||
LoadVar(S, x);
|
||||
if (x < 0) error(S, "corrupted");
|
||||
return x;
|
||||
}
|
||||
|
||||
static lua_Number LoadNumber(LoadState *S)
|
||||
{
|
||||
static lua_Number LoadNumber(LoadState *S) {
|
||||
lua_Number x;
|
||||
LoadVar(S, x);
|
||||
return x;
|
||||
}
|
||||
|
||||
static TString *LoadString(LoadState *S)
|
||||
{
|
||||
static TString *LoadString(LoadState *S) {
|
||||
size_t size;
|
||||
LoadVar(S, size);
|
||||
if (size == 0)
|
||||
|
@ -82,8 +76,7 @@ static TString *LoadString(LoadState *S)
|
|||
}
|
||||
}
|
||||
|
||||
static void LoadCode(LoadState *S, Proto *f)
|
||||
{
|
||||
static void LoadCode(LoadState *S, Proto *f) {
|
||||
int n = LoadInt(S);
|
||||
f->code = luaM_newvector(S->L, n, Instruction);
|
||||
f->sizecode = n;
|
||||
|
@ -92,8 +85,7 @@ static void LoadCode(LoadState *S, Proto *f)
|
|||
|
||||
static void LoadFunction(LoadState *S, Proto *f);
|
||||
|
||||
static void LoadConstants(LoadState *S, Proto *f)
|
||||
{
|
||||
static void LoadConstants(LoadState *S, Proto *f) {
|
||||
int i, n;
|
||||
n = LoadInt(S);
|
||||
f->k = luaM_newvector(S->L, n, TValue);
|
||||
|
@ -129,8 +121,7 @@ static void LoadConstants(LoadState *S, Proto *f)
|
|||
}
|
||||
}
|
||||
|
||||
static void LoadUpvalues(LoadState *S, Proto *f)
|
||||
{
|
||||
static void LoadUpvalues(LoadState *S, Proto *f) {
|
||||
int i, n;
|
||||
n = LoadInt(S);
|
||||
f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
|
||||
|
@ -142,8 +133,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;
|
||||
f->source = LoadString(S);
|
||||
n = LoadInt(S);
|
||||
|
@ -163,8 +153,7 @@ static void LoadDebug(LoadState *S, Proto *f)
|
|||
for (i = 0; i < n; i++) f->upvalues[i].name = LoadString(S);
|
||||
}
|
||||
|
||||
static void LoadFunction(LoadState *S, Proto *f)
|
||||
{
|
||||
static void LoadFunction(LoadState *S, Proto *f) {
|
||||
f->linedefined = LoadInt(S);
|
||||
f->lastlinedefined = LoadInt(S);
|
||||
f->numparams = LoadByte(S);
|
||||
|
@ -182,8 +171,7 @@ static void LoadFunction(LoadState *S, Proto *f)
|
|||
#define N2 N1+2
|
||||
#define N3 N2+6
|
||||
|
||||
static void LoadHeader(LoadState *S)
|
||||
{
|
||||
static void LoadHeader(LoadState *S) {
|
||||
lu_byte h[LUAC_HEADERSIZE];
|
||||
lu_byte s[LUAC_HEADERSIZE];
|
||||
luaU_header(h);
|
||||
|
@ -199,8 +187,7 @@ static void LoadHeader(LoadState *S)
|
|||
/*
|
||||
** load precompiled chunk
|
||||
*/
|
||||
Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name)
|
||||
{
|
||||
Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name) {
|
||||
LoadState S;
|
||||
Closure *cl;
|
||||
if (*name == '@' || *name == '=')
|
||||
|
@ -237,8 +224,7 @@ Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name)
|
|||
* if you change the code below be sure to update LoadHeader and FORMAT above
|
||||
* and LUAC_HEADERSIZE in lundump.h
|
||||
*/
|
||||
void luaU_header(lu_byte *h)
|
||||
{
|
||||
void luaU_header(lu_byte *h) {
|
||||
int x = 1;
|
||||
memcpy(h, LUA_SIGNATURE, sizeof(LUA_SIGNATURE) - sizeof(char));
|
||||
h += sizeof(LUA_SIGNATURE) - sizeof(char);
|
||||
|
|
86
liblua/lvm.c
86
liblua/lvm.c
|
@ -32,8 +32,7 @@
|
|||
#define MAXTAGLOOP 100
|
||||
|
||||
|
||||
const TValue *luaV_tonumber(const TValue *obj, TValue *n)
|
||||
{
|
||||
const TValue *luaV_tonumber(const TValue *obj, TValue *n) {
|
||||
lua_Number num;
|
||||
if (ttisnumber(obj)) return obj;
|
||||
if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
|
||||
|
@ -44,8 +43,7 @@ const TValue *luaV_tonumber(const TValue *obj, TValue *n)
|
|||
}
|
||||
|
||||
|
||||
int luaV_tostring(lua_State *L, StkId obj)
|
||||
{
|
||||
int luaV_tostring(lua_State *L, StkId obj) {
|
||||
if (!ttisnumber(obj))
|
||||
return 0;
|
||||
else {
|
||||
|
@ -58,8 +56,7 @@ int luaV_tostring(lua_State *L, StkId obj)
|
|||
}
|
||||
|
||||
|
||||
static void traceexec(lua_State *L)
|
||||
{
|
||||
static void traceexec(lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
lu_byte mask = L->hookmask;
|
||||
int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
|
||||
|
@ -76,8 +73,8 @@ static void traceexec(lua_State *L)
|
|||
int npc = pcRel(ci->u.l.savedpc, p);
|
||||
int newline = getfuncline(p, npc);
|
||||
if (npc == 0 || /* call linehook when enter a new function, */
|
||||
ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
|
||||
newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
|
||||
ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
|
||||
newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
|
||||
luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
|
||||
}
|
||||
L->oldpc = ci->u.l.savedpc;
|
||||
|
@ -93,8 +90,7 @@ static void traceexec(lua_State *L)
|
|||
|
||||
|
||||
static void callTM(lua_State *L, const TValue *f, const TValue *p1,
|
||||
const TValue *p2, TValue *p3, int hasres)
|
||||
{
|
||||
const TValue *p2, TValue *p3, int hasres) {
|
||||
ptrdiff_t result = savestack(L, p3);
|
||||
setobj2s(L, L->top++, f); /* push function */
|
||||
setobj2s(L, L->top++, p1); /* 1st argument */
|
||||
|
@ -110,8 +106,7 @@ static void callTM(lua_State *L, const TValue *f, const TValue *p1,
|
|||
}
|
||||
|
||||
|
||||
void luaV_gettable(lua_State *L, const TValue *t, TValue *key, StkId val)
|
||||
{
|
||||
void luaV_gettable(lua_State *L, const TValue *t, TValue *key, StkId val) {
|
||||
int loop;
|
||||
for (loop = 0; loop < MAXTAGLOOP; loop++) {
|
||||
const TValue *tm;
|
||||
|
@ -119,7 +114,7 @@ void luaV_gettable(lua_State *L, const TValue *t, TValue *key, StkId val)
|
|||
Table *h = hvalue(t);
|
||||
const TValue *res = luaH_get(h, key); /* do a primitive get */
|
||||
if (!ttisnil(res) || /* result is not nil? */
|
||||
(tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
|
||||
(tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
|
||||
setobj2s(L, val, res);
|
||||
return;
|
||||
}
|
||||
|
@ -136,8 +131,7 @@ void luaV_gettable(lua_State *L, const TValue *t, TValue *key, StkId val)
|
|||
}
|
||||
|
||||
|
||||
void luaV_settable(lua_State *L, const TValue *t, TValue *key, StkId val)
|
||||
{
|
||||
void luaV_settable(lua_State *L, const TValue *t, TValue *key, StkId val) {
|
||||
int loop;
|
||||
for (loop = 0; loop < MAXTAGLOOP; loop++) {
|
||||
const TValue *tm;
|
||||
|
@ -147,13 +141,13 @@ void luaV_settable(lua_State *L, const TValue *t, TValue *key, StkId val)
|
|||
/* if previous value is not nil, there must be a previous entry
|
||||
in the table; moreover, a metamethod has no relevance */
|
||||
if (!ttisnil(oldval) ||
|
||||
/* previous value is nil; must check the metamethod */
|
||||
((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
|
||||
/* no metamethod; is there a previous entry in the table? */
|
||||
(oldval != luaO_nilobject ||
|
||||
/* no previous entry; must create one. (The next test is
|
||||
always true; we only need the assignment.) */
|
||||
(oldval = luaH_newkey(L, h, key), 1)))) {
|
||||
/* previous value is nil; must check the metamethod */
|
||||
((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
|
||||
/* no metamethod; is there a previous entry in the table? */
|
||||
(oldval != luaO_nilobject ||
|
||||
/* no previous entry; must create one. (The next test is
|
||||
always true; we only need the assignment.) */
|
||||
(oldval = luaH_newkey(L, h, key), 1)))) {
|
||||
/* no metamethod and (now) there is an entry with given key */
|
||||
setobj2t(L, oldval, val); /* assign new value to that entry */
|
||||
invalidateTMcache(h);
|
||||
|
@ -176,8 +170,7 @@ void luaV_settable(lua_State *L, const TValue *t, TValue *key, StkId val)
|
|||
|
||||
|
||||
static int call_binTM(lua_State *L, const TValue *p1, const TValue *p2,
|
||||
StkId res, TMS event)
|
||||
{
|
||||
StkId res, TMS event) {
|
||||
const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
|
||||
if (ttisnil(tm))
|
||||
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
||||
|
@ -188,8 +181,7 @@ static int call_binTM(lua_State *L, const TValue *p1, const TValue *p2,
|
|||
|
||||
|
||||
static const TValue *get_equalTM(lua_State *L, Table *mt1, Table *mt2,
|
||||
TMS event)
|
||||
{
|
||||
TMS event) {
|
||||
const TValue *tm1 = fasttm(L, mt1, event);
|
||||
const TValue *tm2;
|
||||
if (tm1 == NULL) return NULL; /* no metamethod */
|
||||
|
@ -203,8 +195,7 @@ static const TValue *get_equalTM(lua_State *L, Table *mt1, Table *mt2,
|
|||
|
||||
|
||||
static int call_orderTM(lua_State *L, const TValue *p1, const TValue *p2,
|
||||
TMS event)
|
||||
{
|
||||
TMS event) {
|
||||
if (!call_binTM(L, p1, p2, L->top, event))
|
||||
return -1; /* no metamethod */
|
||||
else
|
||||
|
@ -212,8 +203,7 @@ static int call_orderTM(lua_State *L, const TValue *p1, const TValue *p2,
|
|||
}
|
||||
|
||||
|
||||
static int l_strcmp(const TString *ls, const TString *rs)
|
||||
{
|
||||
static int l_strcmp(const TString *ls, const TString *rs) {
|
||||
const char *l = getstr(ls);
|
||||
size_t ll = ls->tsv.len;
|
||||
const char *r = getstr(rs);
|
||||
|
@ -238,8 +228,7 @@ static int l_strcmp(const TString *ls, const TString *rs)
|
|||
}
|
||||
|
||||
|
||||
int luaV_lessthan(lua_State *L, const TValue *l, const TValue *r)
|
||||
{
|
||||
int luaV_lessthan(lua_State *L, const TValue *l, const TValue *r) {
|
||||
int res;
|
||||
if (ttisnumber(l) && ttisnumber(r))
|
||||
return luai_numlt(L, nvalue(l), nvalue(r));
|
||||
|
@ -251,8 +240,7 @@ int luaV_lessthan(lua_State *L, const TValue *l, const TValue *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) {
|
||||
int res;
|
||||
if (ttisnumber(l) && ttisnumber(r))
|
||||
return luai_numle(L, nvalue(l), nvalue(r));
|
||||
|
@ -269,8 +257,7 @@ int luaV_lessequal(lua_State *L, const TValue *l, const TValue *r)
|
|||
/*
|
||||
** equality of Lua values. 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;
|
||||
lua_assert(ttisequal(t1, t2));
|
||||
switch (ttype(t1)) {
|
||||
|
@ -310,8 +297,7 @@ int luaV_equalobj_(lua_State *L, const TValue *t1, const TValue *t2)
|
|||
}
|
||||
|
||||
|
||||
void luaV_concat(lua_State *L, int total)
|
||||
{
|
||||
void luaV_concat(lua_State *L, int total) {
|
||||
lua_assert(total >= 2);
|
||||
do {
|
||||
StkId top = L->top;
|
||||
|
@ -351,8 +337,7 @@ void luaV_concat(lua_State *L, int total)
|
|||
}
|
||||
|
||||
|
||||
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 (ttypenv(rb)) {
|
||||
case LUA_TTABLE: {
|
||||
|
@ -378,12 +363,11 @@ void luaV_objlen(lua_State *L, StkId ra, const TValue *rb)
|
|||
|
||||
|
||||
void luaV_arith(lua_State *L, StkId ra, const TValue *rb,
|
||||
const TValue *rc, TMS op)
|
||||
{
|
||||
const TValue *rc, TMS op) {
|
||||
TValue tempb, tempc;
|
||||
const TValue *b, *c;
|
||||
if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
|
||||
(c = luaV_tonumber(rc, &tempc)) != NULL) {
|
||||
(c = luaV_tonumber(rc, &tempc)) != NULL) {
|
||||
lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
|
||||
setnvalue(ra, res);
|
||||
} else if (!call_binTM(L, rb, rc, ra, op))
|
||||
|
@ -396,8 +380,7 @@ void luaV_arith(lua_State *L, StkId ra, const TValue *rb,
|
|||
** whether there is a cached closure with the same upvalues needed by
|
||||
** new closure to be created.
|
||||
*/
|
||||
static Closure *getcached(Proto *p, UpVal **encup, StkId base)
|
||||
{
|
||||
static Closure *getcached(Proto *p, UpVal **encup, StkId base) {
|
||||
Closure *c = p->cache;
|
||||
if (c != NULL) { /* is there a cached closure? */
|
||||
int nup = p->sizeupvalues;
|
||||
|
@ -420,8 +403,7 @@ static Closure *getcached(Proto *p, UpVal **encup, StkId base)
|
|||
** original value of that field.
|
||||
*/
|
||||
static void pushclosure(lua_State *L, Proto *p, UpVal **encup, StkId base,
|
||||
StkId ra)
|
||||
{
|
||||
StkId ra) {
|
||||
int nup = p->sizeupvalues;
|
||||
Upvaldesc *uv = p->upvalues;
|
||||
int i;
|
||||
|
@ -442,8 +424,7 @@ static void pushclosure(lua_State *L, Proto *p, UpVal **encup, StkId base,
|
|||
/*
|
||||
** finish execution of an opcode interrupted by an yield
|
||||
*/
|
||||
void luaV_finishOp(lua_State *L)
|
||||
{
|
||||
void luaV_finishOp(lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
StkId base = ci->u.l.base;
|
||||
Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
|
||||
|
@ -471,7 +452,7 @@ void luaV_finishOp(lua_State *L)
|
|||
/* metamethod should not be called when operand is K */
|
||||
lua_assert(!ISK(GETARG_B(inst)));
|
||||
if (op == OP_LE && /* "<=" using "<" instead? */
|
||||
ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
|
||||
ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
|
||||
res = !res; /* invert result */
|
||||
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
|
||||
if (res != GETARG_A(inst)) /* condition failed? */
|
||||
|
@ -567,8 +548,7 @@ void luaV_finishOp(lua_State *L)
|
|||
#define vmcase(l,b) case l: {b} break;
|
||||
#define vmcasenb(l,b) case l: {b} /* nb = no break */
|
||||
|
||||
void luaV_execute(lua_State *L)
|
||||
{
|
||||
void luaV_execute(lua_State *L) {
|
||||
CallInfo *ci = L->ci;
|
||||
LClosure *cl;
|
||||
TValue *k;
|
||||
|
@ -583,7 +563,7 @@ newframe: /* reentry point when frame changes (call/return) */
|
|||
Instruction i = *(ci->u.l.savedpc++);
|
||||
StkId ra;
|
||||
if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
|
||||
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
|
||||
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
|
||||
Protect(traceexec(L));
|
||||
}
|
||||
/* WARNING: several calls may realloc the stack and invalidate `ra' */
|
||||
|
|
|
@ -18,8 +18,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;
|
||||
|
@ -34,8 +33,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,8 +43,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? */
|
||||
|
@ -68,8 +65,7 @@ size_t luaZ_read(ZIO *z, void *b, size_t n)
|
|||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
char *luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n)
|
||||
{
|
||||
char *luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n) {
|
||||
if (n > buff->buffsize) {
|
||||
if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
|
||||
luaZ_resizebuffer(L, buff, n);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue