diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-06 14:06:47 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-06 14:06:47 -0300 |
| commit | 314c6057b785cd94ac88905ccfce61724107d66b (patch) | |
| tree | 6948ee4fec2cacb850307dab1632bfdb4f777459 | |
| parent | d39ea8b3ce684728c1ad5005192766d39d2e8baa (diff) | |
| download | lua-314c6057b785cd94ac88905ccfce61724107d66b.tar.gz lua-314c6057b785cd94ac88905ccfce61724107d66b.tar.bz2 lua-314c6057b785cd94ac88905ccfce61724107d66b.zip | |
Avoid any code before locks in the API
For consistency in the C API, avoid any initializations before
callling lua_lock.
| -rw-r--r-- | lapi.c | 26 | ||||
| -rw-r--r-- | ldo.c | 3 | ||||
| -rw-r--r-- | lstate.c | 5 |
3 files changed, 22 insertions, 12 deletions
| @@ -97,8 +97,9 @@ static StkId index2stack (lua_State *L, int idx) { | |||
| 97 | 97 | ||
| 98 | LUA_API int lua_checkstack (lua_State *L, int n) { | 98 | LUA_API int lua_checkstack (lua_State *L, int n) { |
| 99 | int res; | 99 | int res; |
| 100 | CallInfo *ci = L->ci; | 100 | CallInfo *ci; |
| 101 | lua_lock(L); | 101 | lua_lock(L); |
| 102 | ci = L->ci; | ||
| 102 | api_check(L, n >= 0, "negative 'n'"); | 103 | api_check(L, n >= 0, "negative 'n'"); |
| 103 | if (L->stack_last - L->top > n) /* stack large enough? */ | 104 | if (L->stack_last - L->top > n) /* stack large enough? */ |
| 104 | res = 1; /* yes; check is OK */ | 105 | res = 1; /* yes; check is OK */ |
| @@ -170,10 +171,12 @@ LUA_API int lua_gettop (lua_State *L) { | |||
| 170 | 171 | ||
| 171 | 172 | ||
| 172 | LUA_API void lua_settop (lua_State *L, int idx) { | 173 | LUA_API void lua_settop (lua_State *L, int idx) { |
| 173 | CallInfo *ci = L->ci; | 174 | CallInfo *ci; |
| 174 | StkId func = ci->func; | 175 | StkId func; |
| 175 | ptrdiff_t diff; /* difference for new top */ | 176 | ptrdiff_t diff; /* difference for new top */ |
| 176 | lua_lock(L); | 177 | lua_lock(L); |
| 178 | ci = L->ci; | ||
| 179 | func = ci->func; | ||
| 177 | if (idx >= 0) { | 180 | if (idx >= 0) { |
| 178 | api_check(L, idx <= ci->top - (func + 1), "new top too large"); | 181 | api_check(L, idx <= ci->top - (func + 1), "new top too large"); |
| 179 | diff = ((func + 1) + idx) - L->top; | 182 | diff = ((func + 1) + idx) - L->top; |
| @@ -376,20 +379,22 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { | |||
| 376 | 379 | ||
| 377 | 380 | ||
| 378 | LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { | 381 | LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { |
| 379 | TValue *o = index2value(L, idx); | 382 | TValue *o; |
| 383 | lua_lock(L); | ||
| 384 | o = index2value(L, idx); | ||
| 380 | if (!ttisstring(o)) { | 385 | if (!ttisstring(o)) { |
| 381 | if (!cvt2str(o)) { /* not convertible? */ | 386 | if (!cvt2str(o)) { /* not convertible? */ |
| 382 | if (len != NULL) *len = 0; | 387 | if (len != NULL) *len = 0; |
| 388 | lua_unlock(L); | ||
| 383 | return NULL; | 389 | return NULL; |
| 384 | } | 390 | } |
| 385 | lua_lock(L); /* 'luaO_tostring' may create a new string */ | ||
| 386 | luaO_tostring(L, o); | 391 | luaO_tostring(L, o); |
| 387 | luaC_checkGC(L); | 392 | luaC_checkGC(L); |
| 388 | o = index2value(L, idx); /* previous call may reallocate the stack */ | 393 | o = index2value(L, idx); /* previous call may reallocate the stack */ |
| 389 | lua_unlock(L); | ||
| 390 | } | 394 | } |
| 391 | if (len != NULL) | 395 | if (len != NULL) |
| 392 | *len = vslen(o); | 396 | *len = vslen(o); |
| 397 | lua_unlock(L); | ||
| 393 | return svalue(o); | 398 | return svalue(o); |
| 394 | } | 399 | } |
| 395 | 400 | ||
| @@ -625,8 +630,9 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { | |||
| 625 | 630 | ||
| 626 | 631 | ||
| 627 | LUA_API int lua_getglobal (lua_State *L, const char *name) { | 632 | LUA_API int lua_getglobal (lua_State *L, const char *name) { |
| 628 | Table *reg = hvalue(&G(L)->l_registry); | 633 | Table *reg; |
| 629 | lua_lock(L); | 634 | lua_lock(L); |
| 635 | reg = hvalue(&G(L)->l_registry); | ||
| 630 | return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); | 636 | return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); |
| 631 | } | 637 | } |
| 632 | 638 | ||
| @@ -805,8 +811,9 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { | |||
| 805 | 811 | ||
| 806 | 812 | ||
| 807 | LUA_API void lua_setglobal (lua_State *L, const char *name) { | 813 | LUA_API void lua_setglobal (lua_State *L, const char *name) { |
| 808 | Table *reg = hvalue(&G(L)->l_registry); | 814 | Table *reg; |
| 809 | lua_lock(L); /* unlock done in 'auxsetstr' */ | 815 | lua_lock(L); /* unlock done in 'auxsetstr' */ |
| 816 | reg = hvalue(&G(L)->l_registry); | ||
| 810 | auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); | 817 | auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); |
| 811 | } | 818 | } |
| 812 | 819 | ||
| @@ -1094,8 +1101,9 @@ LUA_API int lua_status (lua_State *L) { | |||
| 1094 | LUA_API int lua_gc (lua_State *L, int what, ...) { | 1101 | LUA_API int lua_gc (lua_State *L, int what, ...) { |
| 1095 | va_list argp; | 1102 | va_list argp; |
| 1096 | int res = 0; | 1103 | int res = 0; |
| 1097 | global_State *g = G(L); | 1104 | global_State *g; |
| 1098 | lua_lock(L); | 1105 | lua_lock(L); |
| 1106 | g = G(L); | ||
| 1099 | va_start(argp, what); | 1107 | va_start(argp, what); |
| 1100 | switch (what) { | 1108 | switch (what) { |
| 1101 | case LUA_GCSTOP: { | 1109 | case LUA_GCSTOP: { |
| @@ -705,9 +705,10 @@ LUA_API int lua_isyieldable (lua_State *L) { | |||
| 705 | 705 | ||
| 706 | LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, | 706 | LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, |
| 707 | lua_KFunction k) { | 707 | lua_KFunction k) { |
| 708 | CallInfo *ci = L->ci; | 708 | CallInfo *ci; |
| 709 | luai_userstateyield(L, nresults); | 709 | luai_userstateyield(L, nresults); |
| 710 | lua_lock(L); | 710 | lua_lock(L); |
| 711 | ci = L->ci; | ||
| 711 | api_checknelems(L, nresults); | 712 | api_checknelems(L, nresults); |
| 712 | if (unlikely(!yieldable(L))) { | 713 | if (unlikely(!yieldable(L))) { |
| 713 | if (L != G(L)->mainthread) | 714 | if (L != G(L)->mainthread) |
| @@ -318,9 +318,10 @@ static void close_state (lua_State *L) { | |||
| 318 | 318 | ||
| 319 | 319 | ||
| 320 | LUA_API lua_State *lua_newthread (lua_State *L) { | 320 | LUA_API lua_State *lua_newthread (lua_State *L) { |
| 321 | global_State *g = G(L); | 321 | global_State *g; |
| 322 | lua_State *L1; | 322 | lua_State *L1; |
| 323 | lua_lock(L); | 323 | lua_lock(L); |
| 324 | g = G(L); | ||
| 324 | luaC_checkGC(L); | 325 | luaC_checkGC(L); |
| 325 | /* create new thread */ | 326 | /* create new thread */ |
| 326 | L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; | 327 | L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; |
| @@ -437,8 +438,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
| 437 | 438 | ||
| 438 | 439 | ||
| 439 | LUA_API void lua_close (lua_State *L) { | 440 | LUA_API void lua_close (lua_State *L) { |
| 440 | L = G(L)->mainthread; /* only the main thread can be closed */ | ||
| 441 | lua_lock(L); | 441 | lua_lock(L); |
| 442 | L = G(L)->mainthread; /* only the main thread can be closed */ | ||
| 442 | close_state(L); | 443 | close_state(L); |
| 443 | } | 444 | } |
| 444 | 445 | ||
