@@ -200,7 +200,7 @@ static int new_varkind (LexState *ls, TString *name, lu_byte kind) {
200200 luaY_checklimit (fs , dyd -> actvar .n + 1 - fs -> firstlocal ,
201201 MAXVARS , "local variables" );
202202 luaM_growvector (L , dyd -> actvar .arr , dyd -> actvar .n + 1 ,
203- dyd -> actvar .size , Vardesc , SHRT_MAX , "local variables " );
203+ dyd -> actvar .size , Vardesc , SHRT_MAX , "variable declarationss " );
204204 var = & dyd -> actvar .arr [dyd -> actvar .n ++ ];
205205 var -> vd .kind = kind ; /* default */
206206 var -> vd .name = name ;
@@ -276,7 +276,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) {
276276static void init_var (FuncState * fs , expdesc * e , int vidx ) {
277277 e -> f = e -> t = NO_JUMP ;
278278 e -> k = VLOCAL ;
279- e -> u .var .vidx = cast (unsigned short , vidx );
279+ e -> u .var .vidx = cast (short , vidx );
280280 e -> u .var .ridx = getlocalvardesc (fs , vidx )-> vd .ridx ;
281281}
282282
@@ -304,8 +304,16 @@ static void check_readonly (LexState *ls, expdesc *e) {
304304 varname = up -> name ;
305305 break ;
306306 }
307+ case VINDEXUP : case VINDEXSTR : case VINDEXED : {
308+ int vidx = e -> u .ind .vidx ;
309+ /* is it a read-only declared global? */
310+ if (vidx != -1 && ls -> dyd -> actvar .arr [vidx ].vd .kind == GDKCONST )
311+ varname = ls -> dyd -> actvar .arr [vidx ].vd .name ;
312+ break ;
313+ }
307314 default :
308- return ; /* other cases cannot be read-only */
315+ lua_assert (e -> k == VINDEXI ); /* this one doesn't need any check */
316+ return ; /* integer index cannot be read-only */
309317 }
310318 if (varname )
311319 luaK_semerror (ls , "attempt to assign to const variable '%s'" ,
@@ -391,7 +399,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
391399
392400
393401/*
394- ** Look for an active local variable with the name 'n' in the
402+ ** Look for an active variable with the name 'n' in the
395403** function 'fs'. If found, initialize 'var' with it and return
396404** its expression kind; otherwise return -1.
397405*/
@@ -403,7 +411,7 @@ static int searchvar (FuncState *fs, TString *n, expdesc *var) {
403411 if (vd -> vd .kind == RDKCTC ) /* compile-time constant? */
404412 init_exp (var , VCONST , fs -> firstlocal + i );
405413 else if (vd -> vd .kind == GDKREG || vd -> vd .kind == GDKCONST )
406- init_exp (var , VGLOBAL , i );
414+ init_exp (var , VGLOBAL , fs -> firstlocal + i );
407415 else /* local variable */
408416 init_var (fs , var , i );
409417 return cast_int (var -> k );
@@ -475,8 +483,11 @@ static void singlevar (LexState *ls, expdesc *var) {
475483 singlevaraux (fs , varname , var , 1 );
476484 if (var -> k == VGLOBAL ) { /* global name? */
477485 expdesc key ;
486+ int info = var -> u .info ;
487+ lua_assert (info == -1 ||
488+ eqstr (ls -> dyd -> actvar .arr [info ].vd .name , varname ));
478489 /* global by default in the scope of a global declaration? */
479- if (var -> u . info == -1 && fs -> bl -> globdec )
490+ if (info == -1 && fs -> bl -> globdec )
480491 luaK_semerror (ls , "variable '%s' not declared" , getstr (varname ));
481492 singlevaraux (fs , ls -> envn , var , 1 ); /* get environment variable */
482493 if (var -> k == VGLOBAL )
@@ -485,6 +496,7 @@ static void singlevar (LexState *ls, expdesc *var) {
485496 luaK_exp2anyregup (fs , var ); /* but could be a constant */
486497 codestring (& key , varname ); /* key is variable name */
487498 luaK_indexed (fs , var , & key ); /* env[varname] */
499+ var -> u .ind .vidx = cast (short , info ); /* mark it as a declared global */
488500 }
489501}
490502
0 commit comments