static int singlevaraux(FuncState* fs, TString* n, expdesc* var, int base) { if (fs == NULL) /* no more levels? */ { init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ return VGLOBAL; } else { int v = searchvar(fs, n); /* look up at current level */ if (v >= 0) { init_exp(var, VLOCAL, v); if (!base) markupval(fs, v); /* local will be used as an upval */ return VLOCAL; } else /* not found at current level; try upper one */ { if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) return VGLOBAL; var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ var->k = VUPVAL; /* upvalue in this level */ return VUPVAL; } } }
/* Find variable with given name 'n'. If it is an upvalue, add this upvalue into all intermediate functions. */ static int singlevaraux(FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ return VVOID; /* default is global */ else { int v = searchvar(fs, n); /* look up locals at current level */ if (v >= 0) /* found? */ { init_exp(var, VLOCAL, v); /* variable is local */ if (!base) markupval(fs, v); /* local will be used as an upval */ return VLOCAL; } else /* not found as local at current level; try upvalues */ { int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) /* not found? */ { if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */ return VVOID; /* not found; is a global */ /* else was LOCAL or UPVAL */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ } init_exp(var, VUPVAL, idx); return VUPVAL; } } }
static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ else { int v = searchvar(fs, n); /* look up at current level */ if (v >= 0) { init_exp(var, VLOCAL, v); if (!base) markupval(fs, v); /* local will be used as an upval */ } else { /* not found at current level; try upper one */ singlevaraux(fs->prev, n, var, 0); if (var->k == VGLOBAL) { if (base) var->info = luaK_stringK(fs, n); /* info points to global name */ } else { /* LOCAL or UPVAL */ var->info = indexupvalue(fs, n, var); var->k = VUPVAL; /* upvalue in this level */ } } } }