@@ -537,19 +537,34 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
537537
538538/*
539539** Add constant 'v' to prototype's list of constants (field 'k').
540+ */
541+ static int addk (FuncState * fs , Proto * f , TValue * v ) {
542+ lua_State * L = fs -> ls -> L ;
543+ int oldsize = f -> sizek ;
544+ int k = fs -> nk ;
545+ luaM_growvector (L , f -> k , k , f -> sizek , TValue , MAXARG_Ax , "constants" );
546+ while (oldsize < f -> sizek )
547+ setnilvalue (& f -> k [oldsize ++ ]);
548+ setobj (L , & f -> k [k ], v );
549+ fs -> nk ++ ;
550+ luaC_barrier (L , f , v );
551+ return k ;
552+ }
553+
554+
555+ /*
540556** Use scanner's table to cache position of constants in constant list
541557** and try to reuse constants. Because some values should not be used
542558** as keys (nil cannot be a key, integer keys can collapse with float
543559** keys), the caller must provide a useful 'key' for indexing the cache.
544560** Note that all functions share the same table, so entering or exiting
545561** a function can make some indices wrong.
546562*/
547- static int addk (FuncState * fs , TValue * key , TValue * v ) {
563+ static int k2proto (FuncState * fs , TValue * key , TValue * v ) {
548564 TValue val ;
549- lua_State * L = fs -> ls -> L ;
550565 Proto * f = fs -> f ;
551566 int tag = luaH_get (fs -> ls -> h , key , & val ); /* query scanner table */
552- int k , oldsize ;
567+ int k ;
553568 if (tag == LUA_VNUMINT ) { /* is there an index there? */
554569 k = cast_int (ivalue (& val ));
555570 /* correct value? (warning: must distinguish floats from integers!) */
@@ -558,17 +573,11 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
558573 return k ; /* reuse index */
559574 }
560575 /* constant not found; create a new entry */
561- oldsize = f -> sizek ;
562- k = fs -> nk ;
563- /* numerical value does not need GC barrier;
576+ k = addk (fs , f , v );
577+ /* cache for reuse; numerical value does not need GC barrier;
564578 table has no metatable, so it does not need to invalidate cache */
565579 setivalue (& val , k );
566- luaH_set (L , fs -> ls -> h , key , & val );
567- luaM_growvector (L , f -> k , k , f -> sizek , TValue , MAXARG_Ax , "constants" );
568- while (oldsize < f -> sizek ) setnilvalue (& f -> k [oldsize ++ ]);
569- setobj (L , & f -> k [k ], v );
570- fs -> nk ++ ;
571- luaC_barrier (L , f , v );
580+ luaH_set (fs -> ls -> L , fs -> ls -> h , key , & val );
572581 return k ;
573582}
574583
@@ -579,7 +588,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
579588static int stringK (FuncState * fs , TString * s ) {
580589 TValue o ;
581590 setsvalue (fs -> ls -> L , & o , s );
582- return addk (fs , & o , & o ); /* use string itself as key */
591+ return k2proto (fs , & o , & o ); /* use string itself as key */
583592}
584593
585594
@@ -589,7 +598,7 @@ static int stringK (FuncState *fs, TString *s) {
589598static int luaK_intK (FuncState * fs , lua_Integer n ) {
590599 TValue o ;
591600 setivalue (& o , n );
592- return addk (fs , & o , & o ); /* use integer itself as key */
601+ return k2proto (fs , & o , & o ); /* use integer itself as key */
593602}
594603
595604/*
@@ -608,7 +617,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
608617 lua_Integer ik ;
609618 setfltvalue (& o , r );
610619 if (!luaV_flttointeger (r , & ik , F2Ieq )) /* not an integral value? */
611- return addk (fs , & o , & o ); /* use number itself as key */
620+ return k2proto (fs , & o , & o ); /* use number itself as key */
612621 else { /* must build an alternative key */
613622 const int nbm = l_floatatt (MANT_DIG );
614623 const lua_Number q = l_mathop (ldexp )(l_mathop (1.0 ), - nbm + 1 );
@@ -618,7 +627,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
618627 /* result is not an integral value, unless value is too large */
619628 lua_assert (!luaV_flttointeger (k , & ik , F2Ieq ) ||
620629 l_mathop (fabs )(r ) >= l_mathop (1e6 ));
621- return addk (fs , & kv , & o );
630+ return k2proto (fs , & kv , & o );
622631 }
623632}
624633
@@ -629,7 +638,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
629638static int boolF (FuncState * fs ) {
630639 TValue o ;
631640 setbfvalue (& o );
632- return addk (fs , & o , & o ); /* use boolean itself as key */
641+ return k2proto (fs , & o , & o ); /* use boolean itself as key */
633642}
634643
635644
@@ -639,7 +648,7 @@ static int boolF (FuncState *fs) {
639648static int boolT (FuncState * fs ) {
640649 TValue o ;
641650 setbtvalue (& o );
642- return addk (fs , & o , & o ); /* use boolean itself as key */
651+ return k2proto (fs , & o , & o ); /* use boolean itself as key */
643652}
644653
645654
@@ -651,7 +660,7 @@ static int nilK (FuncState *fs) {
651660 setnilvalue (& v );
652661 /* cannot use nil as key; instead use table itself to represent nil */
653662 sethvalue (fs -> ls -> L , & k , fs -> ls -> h );
654- return addk (fs , & k , & v );
663+ return k2proto (fs , & k , & v );
655664}
656665
657666
0 commit comments