aboutsummaryrefslogtreecommitdiff
path: root/src/lua/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/lauxlib.c')
-rw-r--r--src/lua/lauxlib.c44
1 files changed, 28 insertions, 16 deletions
diff --git a/src/lua/lauxlib.c b/src/lua/lauxlib.c
index e8fc486..94835ef 100644
--- a/src/lua/lauxlib.c
+++ b/src/lua/lauxlib.c
@@ -190,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
190} 190}
191 191
192 192
193int luaL_typeerror (lua_State *L, int arg, const char *tname) { 193LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
194 const char *msg; 194 const char *msg;
195 const char *typearg; /* name for the type of the actual argument */ 195 const char *typearg; /* name for the type of the actual argument */
196 if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) 196 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,
378** but without 'msg'.) 378** but without 'msg'.)
379*/ 379*/
380LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { 380LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
381 if (!lua_checkstack(L, space)) { 381 if (l_unlikely(!lua_checkstack(L, space))) {
382 if (msg) 382 if (msg)
383 luaL_error(L, "stack overflow (%s)", msg); 383 luaL_error(L, "stack overflow (%s)", msg);
384 else 384 else
@@ -388,20 +388,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
388 388
389 389
390LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { 390LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
391 if (lua_type(L, arg) != t) 391 if (l_unlikely(lua_type(L, arg) != t))
392 tag_error(L, arg, t); 392 tag_error(L, arg, t);
393} 393}
394 394
395 395
396LUALIB_API void luaL_checkany (lua_State *L, int arg) { 396LUALIB_API void luaL_checkany (lua_State *L, int arg) {
397 if (lua_type(L, arg) == LUA_TNONE) 397 if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
398 luaL_argerror(L, arg, "value expected"); 398 luaL_argerror(L, arg, "value expected");
399} 399}
400 400
401 401
402LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { 402LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
403 const char *s = lua_tolstring(L, arg, len); 403 const char *s = lua_tolstring(L, arg, len);
404 if (!s) tag_error(L, arg, LUA_TSTRING); 404 if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
405 return s; 405 return s;
406} 406}
407 407
@@ -420,7 +420,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
420LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { 420LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
421 int isnum; 421 int isnum;
422 lua_Number d = lua_tonumberx(L, arg, &isnum); 422 lua_Number d = lua_tonumberx(L, arg, &isnum);
423 if (!isnum) 423 if (l_unlikely(!isnum))
424 tag_error(L, arg, LUA_TNUMBER); 424 tag_error(L, arg, LUA_TNUMBER);
425 return d; 425 return d;
426} 426}
@@ -442,7 +442,7 @@ static void interror (lua_State *L, int arg) {
442LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { 442LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
443 int isnum; 443 int isnum;
444 lua_Integer d = lua_tointegerx(L, arg, &isnum); 444 lua_Integer d = lua_tointegerx(L, arg, &isnum);
445 if (!isnum) { 445 if (l_unlikely(!isnum)) {
446 interror(L, arg); 446 interror(L, arg);
447 } 447 }
448 return d; 448 return d;
@@ -475,7 +475,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
475 lua_Alloc allocf = lua_getallocf(L, &ud); 475 lua_Alloc allocf = lua_getallocf(L, &ud);
476 UBox *box = (UBox *)lua_touserdata(L, idx); 476 UBox *box = (UBox *)lua_touserdata(L, idx);
477 void *temp = allocf(ud, box->box, box->bsize, newsize); 477 void *temp = allocf(ud, box->box, box->bsize, newsize);
478 if (temp == NULL && newsize > 0) { /* allocation error? */ 478 if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
479 lua_pushliteral(L, "not enough memory"); 479 lua_pushliteral(L, "not enough memory");
480 lua_error(L); /* raise a memory error */ 480 lua_error(L); /* raise a memory error */
481 } 481 }
@@ -516,12 +516,21 @@ static void newbox (lua_State *L) {
516 516
517 517
518/* 518/*
519** Whenever buffer is accessed, slot 'idx' must either be a box (which
520** cannot be NULL) or it is a placeholder for the buffer.
521*/
522#define checkbufferlevel(B,idx) \
523 lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \
524 : lua_touserdata(B->L, idx) == (void*)B)
525
526
527/*
519** Compute new size for buffer 'B', enough to accommodate extra 'sz' 528** Compute new size for buffer 'B', enough to accommodate extra 'sz'
520** bytes. 529** bytes.
521*/ 530*/
522static size_t newbuffsize (luaL_Buffer *B, size_t sz) { 531static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
523 size_t newsize = B->size * 2; /* double buffer size */ 532 size_t newsize = B->size * 2; /* double buffer size */
524 if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ 533 if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
525 return luaL_error(B->L, "buffer too large"); 534 return luaL_error(B->L, "buffer too large");
526 if (newsize < B->n + sz) /* double is not big enough? */ 535 if (newsize < B->n + sz) /* double is not big enough? */
527 newsize = B->n + sz; 536 newsize = B->n + sz;
@@ -531,10 +540,11 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
531 540
532/* 541/*
533** Returns a pointer to a free area with at least 'sz' bytes in buffer 542** Returns a pointer to a free area with at least 'sz' bytes in buffer
534** 'B'. 'boxidx' is the relative position in the stack where the 543** 'B'. 'boxidx' is the relative position in the stack where is the
535** buffer's box is or should be. 544** buffer's box or its placeholder.
536*/ 545*/
537static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { 546static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
547 checkbufferlevel(B, boxidx);
538 if (B->size - B->n >= sz) /* enough space? */ 548 if (B->size - B->n >= sz) /* enough space? */
539 return B->b + B->n; 549 return B->b + B->n;
540 else { 550 else {
@@ -545,6 +555,7 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
545 if (buffonstack(B)) /* buffer already has a box? */ 555 if (buffonstack(B)) /* buffer already has a box? */
546 newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ 556 newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */
547 else { /* no box yet */ 557 else { /* no box yet */
558 lua_remove(L, boxidx); /* remove placeholder */
548 newbox(L); /* create a new box */ 559 newbox(L); /* create a new box */
549 lua_insert(L, boxidx); /* move box to its intended position */ 560 lua_insert(L, boxidx); /* move box to its intended position */
550 lua_toclose(L, boxidx); 561 lua_toclose(L, boxidx);
@@ -581,11 +592,11 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
581 592
582LUALIB_API void luaL_pushresult (luaL_Buffer *B) { 593LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
583 lua_State *L = B->L; 594 lua_State *L = B->L;
595 checkbufferlevel(B, -1);
584 lua_pushlstring(L, B->b, B->n); 596 lua_pushlstring(L, B->b, B->n);
585 if (buffonstack(B)) { 597 if (buffonstack(B))
586 lua_closeslot(L, -2); /* close the box */ 598 lua_closeslot(L, -2); /* close the box */
587 lua_remove(L, -2); /* remove box from the stack */ 599 lua_remove(L, -2); /* remove box or placeholder from the stack */
588 }
589} 600}
590 601
591 602
@@ -620,6 +631,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
620 B->b = B->init.b; 631 B->b = B->init.b;
621 B->n = 0; 632 B->n = 0;
622 B->size = LUAL_BUFFERSIZE; 633 B->size = LUAL_BUFFERSIZE;
634 lua_pushlightuserdata(L, (void*)B); /* push placeholder */
623} 635}
624 636
625 637
@@ -861,7 +873,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
861 int isnum; 873 int isnum;
862 lua_len(L, idx); 874 lua_len(L, idx);
863 l = lua_tointegerx(L, -1, &isnum); 875 l = lua_tointegerx(L, -1, &isnum);
864 if (!isnum) 876 if (l_unlikely(!isnum))
865 luaL_error(L, "object length is not an integer"); 877 luaL_error(L, "object length is not an integer");
866 lua_pop(L, 1); /* remove object */ 878 lua_pop(L, 1); /* remove object */
867 return l; 879 return l;
@@ -1074,7 +1086,7 @@ static void warnfon (void *ud, const char *message, int tocont) {
1074 1086
1075LUALIB_API lua_State *luaL_newstate (void) { 1087LUALIB_API lua_State *luaL_newstate (void) {
1076 lua_State *L = lua_newstate(l_alloc, NULL); 1088 lua_State *L = lua_newstate(l_alloc, NULL);
1077 if (L) { 1089 if (l_likely(L)) {
1078 lua_atpanic(L, &panic); 1090 lua_atpanic(L, &panic);
1079 lua_setwarnf(L, warnfoff, L); /* default is warnings off */ 1091 lua_setwarnf(L, warnfoff, L); /* default is warnings off */
1080 } 1092 }