mirror of
https://github.com/lua/lua
synced 2025-04-14 00:42:53 +03:00
Lua closures go to local, too
This commit is contained in:
parent
50955e27f5
commit
742b7377d3
19
lfunc.c
19
lfunc.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 2.33 2013/08/16 18:55:49 roberto Exp roberto $
|
||||
** $Id: lfunc.c,v 2.34 2013/08/23 13:34:54 roberto Exp roberto $
|
||||
** Auxiliary functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -29,7 +29,8 @@ Closure *luaF_newCclosure (lua_State *L, int n) {
|
||||
|
||||
|
||||
Closure *luaF_newLclosure (lua_State *L, int n) {
|
||||
Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl;
|
||||
Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n),
|
||||
&G(L)->localgc, 0)->cl;
|
||||
c->l.p = NULL;
|
||||
c->l.nupvalues = cast_byte(n);
|
||||
while (n--) c->l.upvals[n] = NULL;
|
||||
@ -38,7 +39,8 @@ Closure *luaF_newLclosure (lua_State *L, int n) {
|
||||
|
||||
|
||||
UpVal *luaF_newupval (lua_State *L) {
|
||||
UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
|
||||
UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal),
|
||||
&G(L)->localupv, 0)->uv;
|
||||
uv->v = &uv->value;
|
||||
setnilvalue(uv->v);
|
||||
return uv;
|
||||
@ -79,8 +81,15 @@ void luaF_close (lua_State *L, StkId level) {
|
||||
else {
|
||||
setobj(L, &uv->value, uv->v); /* move value to upvalue slot */
|
||||
uv->v = &uv->value; /* now current value lives here */
|
||||
gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */
|
||||
g->allgc = o;
|
||||
if (islocal(o)) {
|
||||
gch(o)->next = g->localupv; /* link upvalue into 'localupv' list */
|
||||
g->localupv = o;
|
||||
resetbit(o->gch.marked, LOCALBLACK);
|
||||
}
|
||||
else { /* link upvalue into 'allgc' list */
|
||||
gch(o)->next = g->allgc;
|
||||
g->allgc = o;
|
||||
}
|
||||
valnolocal(uv->v); /* keep local invariant */
|
||||
luaC_checkupvalcolor(g, uv);
|
||||
}
|
||||
|
41
lgc.c
41
lgc.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 2.150 2013/08/21 20:09:51 roberto Exp roberto $
|
||||
** $Id: lgc.c,v 2.151 2013/08/23 13:34:54 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -885,6 +885,17 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void localmarkclosure (LClosure *cl, int bit) {
|
||||
int i;
|
||||
for (i = 0; i < cl->nupvalues; i++) {
|
||||
if (cl->upvals[i]) {
|
||||
l_setbit(cl->upvals[i]->marked, bit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse a thread, local marking all its collectable objects
|
||||
*/
|
||||
@ -893,8 +904,16 @@ static void localmarkthread (lua_State *l) {
|
||||
if (o == NULL)
|
||||
return; /* stack not completely built yet */
|
||||
for (; o < l->top; o++) { /* mark live elements in the stack */
|
||||
if (iscollectable(o))
|
||||
l_setbit(gcvalue(o)->gch.marked, LOCALBLACK);
|
||||
if (iscollectable(o)) {
|
||||
GCObject *obj = gcvalue(o);
|
||||
if (obj->gch.tt == LUA_TLCL && /* is it a Lua closure? */
|
||||
islocal(obj) && /* is it still local? */
|
||||
!testbit(obj->gch.marked, LOCALBLACK)) { /* not visited yet? */
|
||||
/* mark its upvalues as local black */
|
||||
localmarkclosure(gco2lcl(obj), LOCALBLACK);
|
||||
}
|
||||
l_setbit(obj->gch.marked, LOCALBLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -911,14 +930,17 @@ static void localmark (global_State *g) {
|
||||
}
|
||||
|
||||
|
||||
static void localsweep (lua_State *L, global_State *g) {
|
||||
GCObject **p = &g->localgc;
|
||||
static void localsweep (lua_State *L, global_State *g, GCObject **p) {
|
||||
while (*p != NULL) {
|
||||
GCObject *curr = *p;
|
||||
if (!islocal(curr)) { /* is 'curr' no more local? */
|
||||
*p = curr->gch.next; /* remove 'curr' from list */
|
||||
curr->gch.next = g->allgc; /* link 'curr' in 'allgc' list */
|
||||
g->allgc = curr;
|
||||
if (curr->gch.tt == LUA_TLCL) { /* is it a Lua closure? */
|
||||
/* mark its upvalues as non local */
|
||||
localmarkclosure(gco2lcl(curr), LOCALBIT);
|
||||
}
|
||||
}
|
||||
else { /* still local */
|
||||
if (testbit(curr->gch.marked, LOCALBLACK)) { /* locally alive? */
|
||||
@ -926,6 +948,10 @@ static void localsweep (lua_State *L, global_State *g) {
|
||||
p = &curr->gch.next; /* go to next element */
|
||||
}
|
||||
else { /* object is dead */
|
||||
if (curr->gch.tt == LUA_TLCL) { /* is it a Lua closure? */
|
||||
if (gco2lcl(curr)->p->cache == gco2cl(curr))
|
||||
gco2lcl(curr)->p->cache = NULL; /* clear cache */
|
||||
}
|
||||
*p = curr->gch.next; /* remove 'curr' from list */
|
||||
freeobj(L, curr); /* erase 'curr' */
|
||||
}
|
||||
@ -938,7 +964,8 @@ static void luaC_localcollection (lua_State *L) {
|
||||
global_State *g = G(L);
|
||||
lua_assert(g->gcstate == GCSpause);
|
||||
localmark(g);
|
||||
localsweep(L, g);
|
||||
localsweep(L, g, &g->localgc);
|
||||
localsweep(L, g, &g->localupv);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
@ -1009,6 +1036,7 @@ void luaC_freeallobjects (lua_State *L) {
|
||||
g->gckind = KGC_NORMAL;
|
||||
sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */
|
||||
sweepwholelist(L, &g->localgc);
|
||||
sweepwholelist(L, &g->localupv);
|
||||
sweepwholelist(L, &g->allgc);
|
||||
sweepwholelist(L, &g->fixedgc); /* collect fixed objects */
|
||||
lua_assert(g->strt.nuse == 0);
|
||||
@ -1091,6 +1119,7 @@ static lu_mem singlestep (lua_State *L) {
|
||||
}
|
||||
else {
|
||||
sweepwholelist(L, &g->localgc);
|
||||
sweepwholelist(L, &g->localupv);
|
||||
g->gcstate = GCSsweep;
|
||||
return GCLOCALPAUSE / 4; /* some magic for now */
|
||||
}
|
||||
|
4
lstate.c
4
lstate.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 2.104 2013/08/21 20:09:51 roberto Exp roberto $
|
||||
** $Id: lstate.c,v 2.105 2013/08/23 13:34:54 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -289,7 +289,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||
g->version = lua_version(NULL);
|
||||
g->gcstate = GCSpause;
|
||||
g->allgc = NULL;
|
||||
g->localgc = NULL;
|
||||
g->localgc = g->localupv = NULL;
|
||||
g->finobj = NULL;
|
||||
g->tobefnz = NULL;
|
||||
g->fixedgc = NULL;
|
||||
|
3
lstate.h
3
lstate.h
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 2.88 2013/08/22 15:21:48 roberto Exp roberto $
|
||||
** $Id: lstate.h,v 2.89 2013/08/23 13:34:54 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -128,6 +128,7 @@ typedef struct global_State {
|
||||
lu_byte gcrunning; /* true if GC is running */
|
||||
GCObject *allgc; /* list of all collectable objects */
|
||||
GCObject *localgc; /* list of local objects */
|
||||
GCObject *localupv; /* list of local upvalues */
|
||||
GCObject *finobj; /* list of collectable objects with finalizers */
|
||||
GCObject **sweepgc; /* current position of sweep in list 'allgc' */
|
||||
GCObject **sweepfin; /* current position of sweep in list 'finobj' */
|
||||
|
26
ltests.c
26
ltests.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltests.c,v 2.147 2013/08/21 19:21:16 roberto Exp roberto $
|
||||
** $Id: ltests.c,v 2.148 2013/08/22 15:21:48 roberto Exp roberto $
|
||||
** Internal Module for Debugging of the Lua Implementation
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -400,7 +400,8 @@ static void checkobject (global_State *g, GCObject *o, int maybedead) {
|
||||
|
||||
#define TESTGRAYBIT 7
|
||||
|
||||
static void checkgraylist (GCObject *l) {
|
||||
static void checkgraylist (global_State *g, GCObject *l) {
|
||||
UNUSED(g); /* better to keep it available if we need to print an object */
|
||||
while (l) {
|
||||
lua_assert(isgray(l));
|
||||
lua_assert(!testbit(l->gch.marked, TESTGRAYBIT));
|
||||
@ -423,11 +424,11 @@ static void checkgraylist (GCObject *l) {
|
||||
*/
|
||||
static void markgrays (global_State *g) {
|
||||
if (!keepinvariant(g)) return;
|
||||
checkgraylist(g->gray);
|
||||
checkgraylist(g->grayagain);
|
||||
checkgraylist(g->weak);
|
||||
checkgraylist(g->ephemeron);
|
||||
checkgraylist(g->allweak);
|
||||
checkgraylist(g, g->gray);
|
||||
checkgraylist(g, g->grayagain);
|
||||
checkgraylist(g, g->weak);
|
||||
checkgraylist(g, g->ephemeron);
|
||||
checkgraylist(g, g->allweak);
|
||||
}
|
||||
|
||||
|
||||
@ -484,6 +485,17 @@ int lua_checkmemory (lua_State *L) {
|
||||
lua_assert(gch(o)->tt == LUA_TUSERDATA ||
|
||||
gch(o)->tt == LUA_TTABLE);
|
||||
}
|
||||
/* check 'localgc' list */
|
||||
checkgray(g, g->localgc);
|
||||
for (o = g->localgc; o != NULL; o = gch(o)->next) {
|
||||
checkobject(g, o, 1);
|
||||
}
|
||||
/* check 'localupv' list */
|
||||
checkgray(g, g->localupv);
|
||||
for (o = g->localupv; o != NULL; o = gch(o)->next) {
|
||||
lua_assert(gch(o)->tt == LUA_TUPVAL);
|
||||
checkobject(g, o, 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user