diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-17 11:23:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-17 11:23:22 -0300 |
commit | 1c40ff9faafed620aa0458b397bcbfbe19e0f663 (patch) | |
tree | edfb96022feb99244a7a6c4c9d28525e141b783e /lparser.c | |
parent | 7538f3886dfa091d661c56e48ebb1578ced8e467 (diff) | |
download | lua-1c40ff9faafed620aa0458b397bcbfbe19e0f663.tar.gz lua-1c40ff9faafed620aa0458b397bcbfbe19e0f663.tar.bz2 lua-1c40ff9faafed620aa0458b397bcbfbe19e0f663.zip |
Scanner and parser use different tables for constants
Moreover, each function being parsed has its own table.
The code is cleaner when each table is used for one specific purpose:
The scanner uses its table to anchor and unify strings, mapping strings
to themselves; the parser uses it to reuse constants in the code,
mapping constants to their indices in the constant table. A different
table for each task avoids false collisions.
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 7 |
1 files changed, 6 insertions, 1 deletions
@@ -737,6 +737,7 @@ static void codeclosure (LexState *ls, expdesc *v) { | |||
737 | 737 | ||
738 | 738 | ||
739 | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { | 739 | static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { |
740 | lua_State *L = ls->L; | ||
740 | Proto *f = fs->f; | 741 | Proto *f = fs->f; |
741 | fs->prev = ls->fs; /* linked list of funcstates */ | 742 | fs->prev = ls->fs; /* linked list of funcstates */ |
742 | fs->ls = ls; | 743 | fs->ls = ls; |
@@ -757,8 +758,11 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { | |||
757 | fs->firstlabel = ls->dyd->label.n; | 758 | fs->firstlabel = ls->dyd->label.n; |
758 | fs->bl = NULL; | 759 | fs->bl = NULL; |
759 | f->source = ls->source; | 760 | f->source = ls->source; |
760 | luaC_objbarrier(ls->L, f, f->source); | 761 | luaC_objbarrier(L, f, f->source); |
761 | f->maxstacksize = 2; /* registers 0/1 are always valid */ | 762 | f->maxstacksize = 2; /* registers 0/1 are always valid */ |
763 | fs->kcache = luaH_new(L); /* create table for function */ | ||
764 | sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ | ||
765 | luaD_inctop(L); | ||
762 | enterblock(fs, bl, 0); | 766 | enterblock(fs, bl, 0); |
763 | } | 767 | } |
764 | 768 | ||
@@ -780,6 +784,7 @@ static void close_func (LexState *ls) { | |||
780 | luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); | 784 | luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); |
781 | luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); | 785 | luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); |
782 | ls->fs = fs->prev; | 786 | ls->fs = fs->prev; |
787 | L->top.p--; /* pop kcache table */ | ||
783 | luaC_checkGC(L); | 788 | luaC_checkGC(L); |
784 | } | 789 | } |
785 | 790 | ||