@@ -53,6 +53,10 @@ const char lua_ident[] =
5353#define isupvalue (i ) ((i) < LUA_REGISTRYINDEX)
5454
5555
56+ /*
57+ ** Convert an acceptable index to a pointer to its respective value.
58+ ** Non-valid indices return the special nil value 'G(L)->nilvalue'.
59+ */
5660static TValue * index2value (lua_State * L , int idx ) {
5761 CallInfo * ci = L -> ci ;
5862 if (idx > 0 ) {
@@ -70,22 +74,28 @@ static TValue *index2value (lua_State *L, int idx) {
7074 else { /* upvalues */
7175 idx = LUA_REGISTRYINDEX - idx ;
7276 api_check (L , idx <= MAXUPVAL + 1 , "upvalue index too large" );
73- if (ttislcf (s2v (ci -> func ))) /* light C function? */
74- return & G (L )-> nilvalue ; /* it has no upvalues */
75- else {
77+ if (ttisCclosure (s2v (ci -> func ))) { /* C closure? */
7678 CClosure * func = clCvalue (s2v (ci -> func ));
7779 return (idx <= func -> nupvalues ) ? & func -> upvalue [idx - 1 ]
7880 : & G (L )-> nilvalue ;
7981 }
82+ else { /* light C function or Lua function (through a hook)?) */
83+ api_check (L , ttislcf (s2v (ci -> func )), "caller not a C function" );
84+ return & G (L )-> nilvalue ; /* no upvalues */
85+ }
8086 }
8187}
8288
8389
84- static StkId index2stack (lua_State * L , int idx ) {
90+
91+ /*
92+ ** Convert a valid actual index (not a pseudo-index) to its address.
93+ */
94+ l_sinline StkId index2stack (lua_State * L , int idx ) {
8595 CallInfo * ci = L -> ci ;
8696 if (idx > 0 ) {
8797 StkId o = ci -> func + idx ;
88- api_check (L , o < L -> top , "unacceptable index" );
98+ api_check (L , o < L -> top , "invalid index" );
8999 return o ;
90100 }
91101 else { /* non-positive index */
@@ -218,7 +228,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
218228** Note that we move(copy) only the value inside the stack.
219229** (We do not move additional fields that may exist.)
220230*/
221- static void reverse (lua_State * L , StkId from , StkId to ) {
231+ l_sinline void reverse (lua_State * L , StkId from , StkId to ) {
222232 for (; from < to ; from ++ , to -- ) {
223233 TValue temp ;
224234 setobj (L , & temp , s2v (from ));
@@ -438,7 +448,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
438448}
439449
440450
441- static void * touserdata (const TValue * o ) {
451+ l_sinline void * touserdata (const TValue * o ) {
442452 switch (ttype (o )) {
443453 case LUA_TUSERDATA : return getudatamem (uvalue (o ));
444454 case LUA_TLIGHTUSERDATA : return pvalue (o );
@@ -630,7 +640,7 @@ LUA_API int lua_pushthread (lua_State *L) {
630640*/
631641
632642
633- static int auxgetstr (lua_State * L , const TValue * t , const char * k ) {
643+ l_sinline int auxgetstr (lua_State * L , const TValue * t , const char * k ) {
634644 const TValue * slot ;
635645 TString * str = luaS_new (L , k );
636646 if (luaV_fastget (L , t , str , slot , luaH_getstr )) {
@@ -705,7 +715,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
705715}
706716
707717
708- static int finishrawget (lua_State * L , const TValue * val ) {
718+ l_sinline int finishrawget (lua_State * L , const TValue * val ) {
709719 if (isempty (val )) /* avoid copying empty items to the stack */
710720 setnilvalue (s2v (L -> top ));
711721 else
@@ -1126,18 +1136,19 @@ LUA_API int lua_status (lua_State *L) {
11261136LUA_API int lua_gc (lua_State * L , int what , ...) {
11271137 va_list argp ;
11281138 int res = 0 ;
1129- global_State * g ;
1139+ global_State * g = G (L );
1140+ if (g -> gcstp & GCSTPGC ) /* internal stop? */
1141+ return -1 ; /* all options are invalid when stopped */
11301142 lua_lock (L );
1131- g = G (L );
11321143 va_start (argp , what );
11331144 switch (what ) {
11341145 case LUA_GCSTOP : {
1135- g -> gcrunning = 0 ;
1146+ g -> gcstp = GCSTPUSR ; /* stopped by the user */
11361147 break ;
11371148 }
11381149 case LUA_GCRESTART : {
11391150 luaE_setdebt (g , 0 );
1140- g -> gcrunning = 1 ;
1151+ g -> gcstp = 0 ; /* (GCSTPGC must be already zero here) */
11411152 break ;
11421153 }
11431154 case LUA_GCCOLLECT : {
@@ -1156,8 +1167,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11561167 case LUA_GCSTEP : {
11571168 int data = va_arg (argp , int );
11581169 l_mem debt = 1 ; /* =1 to signal that it did an actual step */
1159- lu_byte oldrunning = g -> gcrunning ;
1160- g -> gcrunning = 1 ; /* allow GC to run */
1170+ lu_byte oldstp = g -> gcstp ;
1171+ g -> gcstp = 0 ; /* allow GC to run (GCSTPGC must be zero here) */
11611172 if (data == 0 ) {
11621173 luaE_setdebt (g , 0 ); /* do a basic step */
11631174 luaC_step (L );
@@ -1167,7 +1178,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11671178 luaE_setdebt (g , debt );
11681179 luaC_checkGC (L );
11691180 }
1170- g -> gcrunning = oldrunning ; /* restore previous state */
1181+ g -> gcstp = oldstp ; /* restore previous state */
11711182 if (debt > 0 && g -> gcstate == GCSpause ) /* end of cycle? */
11721183 res = 1 ; /* signal it */
11731184 break ;
@@ -1185,7 +1196,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11851196 break ;
11861197 }
11871198 case LUA_GCISRUNNING : {
1188- res = g -> gcrunning ;
1199+ res = gcrunning ( g ) ;
11891200 break ;
11901201 }
11911202 case LUA_GCGEN : {
0 commit comments