diff options
Diffstat (limited to 'lfunc.c')
-rw-r--r-- | lfunc.c | 34 |
1 files changed, 7 insertions, 27 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lfunc.c,v 2.30 2012/10/03 12:36:46 roberto Exp roberto $ | 2 | ** $Id: lfunc.c,v 2.31 2013/08/05 16:58:28 roberto Exp roberto $ |
3 | ** Auxiliary functions to manipulate prototypes and closures | 3 | ** Auxiliary functions to manipulate prototypes and closures |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -38,7 +38,7 @@ Closure *luaF_newLclosure (lua_State *L, int n) { | |||
38 | 38 | ||
39 | UpVal *luaF_newupval (lua_State *L) { | 39 | UpVal *luaF_newupval (lua_State *L) { |
40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; | 40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; |
41 | uv->v = &uv->u.value; | 41 | uv->v = &uv->value; |
42 | setnilvalue(uv->v); | 42 | setnilvalue(uv->v); |
43 | return uv; | 43 | return uv; |
44 | } | 44 | } |
@@ -51,7 +51,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { | |||
51 | UpVal *uv; | 51 | UpVal *uv; |
52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { | 52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { |
53 | GCObject *o = obj2gco(p); | 53 | GCObject *o = obj2gco(p); |
54 | lua_assert(p->v != &p->u.value); | 54 | lua_assert(p->v != &p->value); |
55 | if (p->v == level) { /* found a corresponding upvalue? */ | 55 | if (p->v == level) { /* found a corresponding upvalue? */ |
56 | if (isdead(g, o)) /* is it dead? */ | 56 | if (isdead(g, o)) /* is it dead? */ |
57 | changewhite(o); /* resurrect it */ | 57 | changewhite(o); /* resurrect it */ |
@@ -62,42 +62,22 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { | |||
62 | /* not found: create a new one */ | 62 | /* not found: create a new one */ |
63 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; | 63 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; |
64 | uv->v = level; /* current value lives in the stack */ | 64 | uv->v = level; /* current value lives in the stack */ |
65 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ | ||
66 | uv->u.l.next = g->uvhead.u.l.next; | ||
67 | uv->u.l.next->u.l.prev = uv; | ||
68 | g->uvhead.u.l.next = uv; | ||
69 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); | ||
70 | return uv; | 65 | return uv; |
71 | } | 66 | } |
72 | 67 | ||
73 | 68 | ||
74 | static void unlinkupval (UpVal *uv) { | ||
75 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); | ||
76 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ | ||
77 | uv->u.l.prev->u.l.next = uv->u.l.next; | ||
78 | } | ||
79 | |||
80 | |||
81 | void luaF_freeupval (lua_State *L, UpVal *uv) { | ||
82 | if (uv->v != &uv->u.value) /* is it open? */ | ||
83 | unlinkupval(uv); /* remove from open list */ | ||
84 | luaM_free(L, uv); /* free upvalue */ | ||
85 | } | ||
86 | |||
87 | |||
88 | void luaF_close (lua_State *L, StkId level) { | 69 | void luaF_close (lua_State *L, StkId level) { |
89 | UpVal *uv; | 70 | UpVal *uv; |
90 | global_State *g = G(L); | 71 | global_State *g = G(L); |
91 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { | 72 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { |
92 | GCObject *o = obj2gco(uv); | 73 | GCObject *o = obj2gco(uv); |
93 | lua_assert(!isblack(o) && uv->v != &uv->u.value); | 74 | lua_assert(!isblack(o) && uv->v != &uv->value); |
94 | L->openupval = uv->next; /* remove from `open' list */ | 75 | L->openupval = uv->next; /* remove from `open' list */ |
95 | if (isdead(g, o)) | 76 | if (isdead(g, o)) |
96 | luaF_freeupval(L, uv); /* free upvalue */ | 77 | luaM_free(L, uv); /* free upvalue */ |
97 | else { | 78 | else { |
98 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ | 79 | setobj(L, &uv->value, uv->v); /* move value to upvalue slot */ |
99 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ | 80 | uv->v = &uv->value; /* now current value lives here */ |
100 | uv->v = &uv->u.value; /* now current value lives here */ | ||
101 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ | 81 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ |
102 | g->allgc = o; | 82 | g->allgc = o; |
103 | luaC_checkupvalcolor(g, uv); | 83 | luaC_checkupvalcolor(g, uv); |