@@ -1875,6 +1875,16 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) {
18751875}
18761876
18771877
1878+ static void checkglobal (LexState * ls , TString * varname , int line ) {
1879+ FuncState * fs = ls -> fs ;
1880+ expdesc var ;
1881+ int k ;
1882+ buildglobal (ls , varname , & var ); /* create global variable in 'var' */
1883+ k = var .u .ind .keystr ; /* index of global name in 'k' */
1884+ luaK_codecheckglobal (fs , & var , k , line );
1885+ }
1886+
1887+
18781888/*
18791889** Recursively traverse list of globals to be initalized. When
18801890** going, generate table description for the global. In the end,
@@ -1883,7 +1893,8 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) {
18831893** the stack to the corresponding table description. 'n' is the variable
18841894** being handled, range [0, nvars - 1].
18851895*/
1886- static void initglobal (LexState * ls , int nvars , int firstidx , int n ) {
1896+ static void initglobal (LexState * ls , int nvars , int firstidx , int n ,
1897+ int line ) {
18871898 if (n == nvars ) { /* traversed all variables? */
18881899 expdesc e ;
18891900 int nexps = explist (ls , & e ); /* read list of expressions */
@@ -1895,8 +1906,9 @@ static void initglobal (LexState *ls, int nvars, int firstidx, int n) {
18951906 TString * varname = getlocalvardesc (fs , firstidx + n )-> vd .name ;
18961907 buildglobal (ls , varname , & var ); /* create global variable in 'var' */
18971908 enterlevel (ls ); /* control recursion depth */
1898- initglobal (ls , nvars , firstidx , n + 1 );
1909+ initglobal (ls , nvars , firstidx , n + 1 , line );
18991910 leavelevel (ls );
1911+ checkglobal (ls , varname , line );
19001912 storevartop (fs , & var );
19011913 }
19021914}
@@ -1913,7 +1925,7 @@ static void globalnames (LexState *ls, lu_byte defkind) {
19131925 nvars ++ ;
19141926 } while (testnext (ls , ',' ));
19151927 if (testnext (ls , '=' )) /* initialization? */
1916- initglobal (ls , nvars , lastidx - nvars + 1 , 0 );
1928+ initglobal (ls , nvars , lastidx - nvars + 1 , 0 , ls -> linenumber );
19171929 fs -> nactvar = cast_short (fs -> nactvar + nvars ); /* activate declaration */
19181930}
19191931
@@ -1943,6 +1955,7 @@ static void globalfunc (LexState *ls, int line) {
19431955 fs -> nactvar ++ ; /* enter its scope */
19441956 buildglobal (ls , fname , & var );
19451957 body (ls , & b , 0 , ls -> linenumber ); /* compile and return closure in 'b' */
1958+ checkglobal (ls , fname , line );
19461959 luaK_storevar (fs , & var , & b );
19471960 luaK_fixline (fs , line ); /* definition "happens" in the first line */
19481961}
0 commit comments