Skip to content

Commit

Permalink
Merge branch 'master' into check_network
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Apr 18, 2024
2 parents a81196b + 1f4d9b3 commit 656201b
Show file tree
Hide file tree
Showing 50 changed files with 1,472 additions and 2,403 deletions.
314 changes: 153 additions & 161 deletions extend/lua/lua/lapi.c

Large diffs are not rendered by default.

19 changes: 5 additions & 14 deletions extend/lua/lua/lapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@


/* Increments 'L->top.p', checking for stack overflows */
#define api_incr_top(L) \
(L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow"))
#define api_incr_top(L) {L->top.p++; \
api_check(L, L->top.p <= L->ci->top.p, \
"stack overflow");}


/*
Expand All @@ -29,18 +30,8 @@

/* Ensure the stack has at least 'n' elements */
#define api_checknelems(L,n) \
api_check(L, (n) < (L->top.p - L->ci->func.p), \
"not enough elements in the stack")


/* Ensure the stack has at least 'n' elements to be popped. (Some
** functions only update a slot after checking it for popping, but that
** is only an optimization for a pop followed by a push.)
*/
#define api_checkpop(L,n) \
api_check(L, (n) < L->top.p - L->ci->func.p && \
L->tbclist.p < L->top.p - (n), \
"not enough free elements in the stack")
api_check(L, (n) < (L->top.p - L->ci->func.p), \
"not enough elements in the stack")


/*
Expand Down
142 changes: 35 additions & 107 deletions extend/lua/lua/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,27 +470,18 @@ typedef struct UBox {
} UBox;


/* Resize the buffer used by a box. Optimize for the common case of
** resizing to the old size. (For instance, __gc will resize the box
** to 0 even after it was closed. 'pushresult' may also resize it to a
** final size that is equal to the one set when the buffer was created.)
*/
static void *resizebox (lua_State *L, int idx, size_t newsize) {
void *ud;
lua_Alloc allocf = lua_getallocf(L, &ud);
UBox *box = (UBox *)lua_touserdata(L, idx);
if (box->bsize == newsize) /* not changing size? */
return box->box; /* keep the buffer */
else {
void *ud;
lua_Alloc allocf = lua_getallocf(L, &ud);
void *temp = allocf(ud, box->box, box->bsize, newsize);
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
lua_pushliteral(L, "not enough memory");
lua_error(L); /* raise a memory error */
}
box->box = temp;
box->bsize = newsize;
return temp;
void *temp = allocf(ud, box->box, box->bsize, newsize);
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
lua_pushliteral(L, "not enough memory");
lua_error(L); /* raise a memory error */
}
box->box = temp;
box->bsize = newsize;
return temp;
}


Expand Down Expand Up @@ -535,15 +526,15 @@ static void newbox (lua_State *L) {

/*
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
** bytes plus one for a terminating zero. (The test for "not big enough"
** also gets the case when the computation of 'newsize' overflows.)
** bytes. (The test for "not big enough" also gets the case when the
** computation of 'newsize' overflows.)
*/
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
if (l_unlikely(MAX_SIZET - sz - 1 < B->n)) /* overflow in (B->n + sz + 1)? */
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
return luaL_error(B->L, "buffer too large");
if (newsize < B->n + sz + 1) /* not big enough? */
newsize = B->n + sz + 1;
if (newsize < B->n + sz) /* not big enough? */
newsize = B->n + sz;
return newsize;
}

Expand Down Expand Up @@ -603,22 +594,9 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
lua_State *L = B->L;
checkbufferlevel(B, -1);
if (!buffonstack(B)) /* using static buffer? */
lua_pushlstring(L, B->b, B->n); /* save result as regular string */
else { /* reuse buffer already allocated */
UBox *box = (UBox *)lua_touserdata(L, -1);
void *ud;
lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */
size_t len = B->n; /* final string length */
char *s;
resizebox(L, -1, len + 1); /* adjust box size to content size */
s = (char*)box->box; /* final buffer address */
s[len] = '\0'; /* add ending zero */
/* clear box, as 'lua_pushextlstring' will take control over buffer */
box->bsize = 0; box->box = NULL;
lua_pushextlstring(L, s, len, allocf, ud);
lua_pushlstring(L, B->b, B->n);
if (buffonstack(B))
lua_closeslot(L, -2); /* close the box */
}
lua_remove(L, -2); /* remove box or placeholder from the stack */
}

Expand Down Expand Up @@ -672,10 +650,13 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
** =======================================================
*/

/* index of free-list header (after the predefined values) */
#define freelist (LUA_RIDX_LAST + 1)

/*
** The previously freed references form a linked list: t[1] is the index
** of a first free index, t[t[1]] is the index of the second element,
** etc. A zero signals the end of the list.
** The previously freed references form a linked list:
** t[freelist] is the index of a first free index, or zero if list is
** empty; t[t[freelist]] is the index of the second element; etc.
*/
LUALIB_API int luaL_ref (lua_State *L, int t) {
int ref;
Expand All @@ -684,18 +665,19 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
}
t = lua_absindex(L, t);
if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */
ref = (int)lua_tointeger(L, -1); /* ref = t[1] */
else { /* first access */
lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */
if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */
ref = 0; /* list is empty */
lua_pushinteger(L, 0); /* initialize as an empty list */
lua_rawseti(L, t, 1); /* ref = t[1] = 0 */
lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */
}
else { /* already initialized */
lua_assert(lua_isinteger(L, -1));
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
}
lua_pop(L, 1); /* remove element from stack */
if (ref != 0) { /* any free element? */
lua_rawgeti(L, t, ref); /* remove it from list */
lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
}
else /* no free elements */
ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
Expand All @@ -707,11 +689,11 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
if (ref >= 0) {
t = lua_absindex(L, t);
lua_rawgeti(L, t, 1);
lua_rawgeti(L, t, freelist);
lua_assert(lua_isinteger(L, -1));
lua_rawseti(L, t, ref); /* t[ref] = t[1] */
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
lua_pushinteger(L, ref);
lua_rawseti(L, t, 1); /* t[1] = ref */
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
}
}

Expand Down Expand Up @@ -1043,14 +1025,9 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
}


/*
** Standard panic funcion just prints an error message. The test
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
*/
static int panic (lua_State *L) {
const char *msg = (lua_type(L, -1) == LUA_TSTRING)
? lua_tostring(L, -1)
: "error object is not a string";
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "error object is not a string";
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
msg);
return 0; /* return to Lua to abort */
Expand Down Expand Up @@ -1114,57 +1091,8 @@ static void warnfon (void *ud, const char *message, int tocont) {
}



/*
** A function to compute an unsigned int with some level of
** randomness. Rely on Address Space Layout Randomization (if present)
** and the current time.
*/
#if !defined(luai_makeseed)

#include <time.h>


/* Size for the buffer, in bytes */
#define BUFSEEDB (sizeof(void*) + sizeof(time_t))

/* Size for the buffer in int's, rounded up */
#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))

/*
** Copy the contents of variable 'v' into the buffer pointed by 'b'.
** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.)
*/
#define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))


static unsigned int luai_makeseed (void) {
unsigned int buff[BUFSEED];
unsigned int res;
unsigned int i;
time_t t = time(NULL);
char *b = (char*)buff;
addbuff(b, b); /* local variable's address */
addbuff(b, t); /* time */
/* fill (rare but possible) remain of the buffer with zeros */
memset(b, 0, sizeof(buff) - BUFSEEDB);
res = buff[0];
for (i = 1; i < BUFSEED; i++)
res ^= (res >> 3) + (res << 7) + buff[i];
return res;
}

#endif


LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
(void)L; /* unused */
return luai_makeseed();
}


LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed());
lua_State *L = lua_newstate(l_alloc, NULL);
if (l_likely(L)) {
lua_atpanic(L, &panic);
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
Expand Down
2 changes: 0 additions & 2 deletions extend/lua/lua/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);

LUALIB_API lua_State *(luaL_newstate) (void);

LUALIB_API unsigned luaL_makeseed (lua_State *L);

LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);

LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
Expand Down
55 changes: 24 additions & 31 deletions extend/lua/lua/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ static int pushmode (lua_State *L, int oldmode) {

static int luaB_collectgarbage (lua_State *L) {
static const char *const opts[] = {"stop", "restart", "collect",
"count", "step", "isrunning", "generational", "incremental",
"param", NULL};
static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC,
LUA_GCPARAM};
"count", "step", "setpause", "setstepmul",
"isrunning", "generational", "incremental", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC};
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
switch (o) {
case LUA_GCCOUNT: {
Expand All @@ -213,35 +213,36 @@ static int luaB_collectgarbage (lua_State *L) {
return 1;
}
case LUA_GCSTEP: {
lua_Integer n = luaL_optinteger(L, 2, 0);
int res = lua_gc(L, o, (int)n);
int step = (int)luaL_optinteger(L, 2, 0);
int res = lua_gc(L, o, step);
checkvalres(res);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCSETPAUSE:
case LUA_GCSETSTEPMUL: {
int p = (int)luaL_optinteger(L, 2, 0);
int previous = lua_gc(L, o, p);
checkvalres(previous);
lua_pushinteger(L, previous);
return 1;
}
case LUA_GCISRUNNING: {
int res = lua_gc(L, o);
checkvalres(res);
lua_pushboolean(L, res);
return 1;
}
case LUA_GCGEN: {
return pushmode(L, lua_gc(L, o));
int minormul = (int)luaL_optinteger(L, 2, 0);
int majormul = (int)luaL_optinteger(L, 3, 0);
return pushmode(L, lua_gc(L, o, minormul, majormul));
}
case LUA_GCINC: {
return pushmode(L, lua_gc(L, o));
}
case LUA_GCPARAM: {
static const char *const params[] = {
"minormul", "majorminor", "minormajor",
"pause", "stepmul", "stepsize", NULL};
static const char pnum[] = {
LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR,
LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE};
int p = pnum[luaL_checkoption(L, 2, NULL, params)];
lua_Integer value = luaL_optinteger(L, 3, -1);
lua_pushinteger(L, lua_gc(L, o, p, (int)value));
return 1;
int pause = (int)luaL_optinteger(L, 2, 0);
int stepmul = (int)luaL_optinteger(L, 3, 0);
int stepsize = (int)luaL_optinteger(L, 4, 0);
return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize));
}
default: {
int res = lua_gc(L, o);
Expand Down Expand Up @@ -336,17 +337,9 @@ static int load_aux (lua_State *L, int status, int envidx) {
}


static const char *getmode (lua_State *L, int idx) {
const char *mode = luaL_optstring(L, idx, "bt");
if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */
luaL_argerror(L, idx, "invalid mode");
return mode;
}


static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = getmode(L, 2);
const char *mode = luaL_optstring(L, 2, NULL);
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
int status = luaL_loadfilex(L, fname, mode);
return load_aux(L, status, env);
Expand Down Expand Up @@ -395,7 +388,7 @@ static int luaB_load (lua_State *L) {
int status;
size_t l;
const char *s = lua_tolstring(L, 1, &l);
const char *mode = getmode(L, 3);
const char *mode = luaL_optstring(L, 3, "bt");
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s);
Expand Down
Loading

0 comments on commit 656201b

Please sign in to comment.