summaryrefslogtreecommitdiff
path: root/libs/liblua/src/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/liblua/src/lauxlib.c')
-rw-r--r--libs/liblua/src/lauxlib.c74
1 files changed, 26 insertions, 48 deletions
diff --git a/libs/liblua/src/lauxlib.c b/libs/liblua/src/lauxlib.c
index 94835ef934..73504389e1 100644
--- a/libs/liblua/src/lauxlib.c
+++ b/libs/liblua/src/lauxlib.c
@@ -190,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
}
-LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
+int luaL_typeerror (lua_State *L, int arg, const char *tname) {
const char *msg;
const char *typearg; /* name for the type of the actual argument */
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
@@ -378,7 +378,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
** but without 'msg'.)
*/
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
- if (l_unlikely(!lua_checkstack(L, space))) {
+ if (!lua_checkstack(L, space)) {
if (msg)
luaL_error(L, "stack overflow (%s)", msg);
else
@@ -388,20 +388,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
- if (l_unlikely(lua_type(L, arg) != t))
+ if (lua_type(L, arg) != t)
tag_error(L, arg, t);
}
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
- if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
+ if (lua_type(L, arg) == LUA_TNONE)
luaL_argerror(L, arg, "value expected");
}
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
const char *s = lua_tolstring(L, arg, len);
- if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
+ if (!s) tag_error(L, arg, LUA_TSTRING);
return s;
}
@@ -420,7 +420,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
int isnum;
lua_Number d = lua_tonumberx(L, arg, &isnum);
- if (l_unlikely(!isnum))
+ if (!isnum)
tag_error(L, arg, LUA_TNUMBER);
return d;
}
@@ -442,7 +442,7 @@ static void interror (lua_State *L, int arg) {
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
int isnum;
lua_Integer d = lua_tointegerx(L, arg, &isnum);
- if (l_unlikely(!isnum)) {
+ if (!isnum) {
interror(L, arg);
}
return d;
@@ -475,7 +475,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
lua_Alloc allocf = lua_getallocf(L, &ud);
UBox *box = (UBox *)lua_touserdata(L, idx);
void *temp = allocf(ud, box->box, box->bsize, newsize);
- if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
+ if (temp == NULL && newsize > 0) { /* allocation error? */
lua_pushliteral(L, "not enough memory");
lua_error(L); /* raise a memory error */
}
@@ -516,21 +516,12 @@ static void newbox (lua_State *L) {
/*
-** Whenever buffer is accessed, slot 'idx' must either be a box (which
-** cannot be NULL) or it is a placeholder for the buffer.
-*/
-#define checkbufferlevel(B,idx) \
- lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \
- : lua_touserdata(B->L, idx) == (void*)B)
-
-
-/*
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
** bytes.
*/
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
size_t newsize = B->size * 2; /* double buffer size */
- if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
+ if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */
return luaL_error(B->L, "buffer too large");
if (newsize < B->n + sz) /* double is not big enough? */
newsize = B->n + sz;
@@ -540,11 +531,10 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
/*
** Returns a pointer to a free area with at least 'sz' bytes in buffer
-** 'B'. 'boxidx' is the relative position in the stack where is the
-** buffer's box or its placeholder.
+** 'B'. 'boxidx' is the relative position in the stack where the
+** buffer's box is or should be.
*/
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
- checkbufferlevel(B, boxidx);
if (B->size - B->n >= sz) /* enough space? */
return B->b + B->n;
else {
@@ -555,9 +545,10 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
if (buffonstack(B)) /* buffer already has a box? */
newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */
else { /* no box yet */
- lua_remove(L, boxidx); /* remove placeholder */
+ lua_pushnil(L); /* reserve slot for final result */
newbox(L); /* create a new box */
- lua_insert(L, boxidx); /* move box to its intended position */
+ /* move box (and slot) to its intended position */
+ lua_rotate(L, boxidx - 1, 2);
lua_toclose(L, boxidx);
newbuff = (char *)resizebox(L, boxidx, newsize);
memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
@@ -592,11 +583,11 @@ 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);
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 */
+ if (buffonstack(B)) {
+ lua_copy(L, -1, -3); /* move string to reserved slot */
+ lua_pop(L, 2); /* pop string and box (closing the box) */
+ }
}
@@ -631,7 +622,6 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
B->b = B->init.b;
B->n = 0;
B->size = LUAL_BUFFERSIZE;
- lua_pushlightuserdata(L, (void*)B); /* push placeholder */
}
@@ -649,14 +639,10 @@ 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)
+/* index of free-list header */
+#define freelist 0
+
-/*
-** 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;
if (lua_isnil(L, -1)) {
@@ -664,16 +650,9 @@ 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, freelist) == LUA_TNIL) { /* first access? */
- ref = 0; /* list is empty */
- lua_pushinteger(L, 0); /* initialize as an empty list */
- lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */
- }
- else { /* already initialized */
- lua_assert(lua_isinteger(L, -1));
- ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
- }
- lua_pop(L, 1); /* remove element from stack */
+ lua_rawgeti(L, t, freelist); /* get first free element */
+ ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
+ lua_pop(L, 1); /* remove it from stack */
if (ref != 0) { /* any free element? */
lua_rawgeti(L, t, ref); /* remove it from list */
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
@@ -689,7 +668,6 @@ 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);
- lua_assert(lua_isinteger(L, -1));
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
lua_pushinteger(L, ref);
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
@@ -873,7 +851,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
int isnum;
lua_len(L, idx);
l = lua_tointegerx(L, -1, &isnum);
- if (l_unlikely(!isnum))
+ if (!isnum)
luaL_error(L, "object length is not an integer");
lua_pop(L, 1); /* remove object */
return l;
@@ -1086,7 +1064,7 @@ static void warnfon (void *ud, const char *message, int tocont) {
LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
- if (l_likely(L)) {
+ if (L) {
lua_atpanic(L, &panic);
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
}