diff options
| author | Li Jin <dragon-fly@qq.com> | 2021-03-03 21:31:01 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2021-03-03 21:33:37 +0800 |
| commit | 1df786307c1983b8ce693e3916081a8bcd4e95ae (patch) | |
| tree | 6c7aeb2198d825877fd3d179c394b7a5c1f06a17 /src/lua/lauxlib.c | |
| parent | 66168b112b707172b9035edf8c1daed469781e06 (diff) | |
| download | yuescript-1df786307c1983b8ce693e3916081a8bcd4e95ae.tar.gz yuescript-1df786307c1983b8ce693e3916081a8bcd4e95ae.tar.bz2 yuescript-1df786307c1983b8ce693e3916081a8bcd4e95ae.zip | |
add new metatable syntax for issue #41, fix reusing local variable issue, update built-in Lua.
Diffstat (limited to '')
| -rw-r--r-- | src/lua/lauxlib.c | 44 |
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 | ||
| 193 | int luaL_typeerror (lua_State *L, int arg, const char *tname) { | 193 | LUALIB_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 | */ |
| 380 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { | 380 | LUALIB_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 | ||
| 390 | LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { | 390 | LUALIB_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 | ||
| 396 | LUALIB_API void luaL_checkany (lua_State *L, int arg) { | 396 | LUALIB_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 | ||
| 402 | LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { | 402 | LUALIB_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, | |||
| 420 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { | 420 | LUALIB_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) { | |||
| 442 | LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { | 442 | LUALIB_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 | */ |
| 522 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { | 531 | static 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 | */ |
| 537 | static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { | 546 | static 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 | ||
| 582 | LUALIB_API void luaL_pushresult (luaL_Buffer *B) { | 593 | LUALIB_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 | ||
| 1075 | LUALIB_API lua_State *luaL_newstate (void) { | 1087 | LUALIB_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 | } |
