diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-11-05 15:43:54 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-11-05 15:43:54 -0200 |
| commit | 9756f56354af72d62b4f63c85000d4ae1db6b130 (patch) | |
| tree | 93d9ddf16bf75b75be809611f7d11e046bfb6927 | |
| parent | b7d5f18d71f691df752e220f844ea613a8f6d722 (diff) | |
| download | lua-9756f56354af72d62b4f63c85000d4ae1db6b130.tar.gz lua-9756f56354af72d62b4f63c85000d4ae1db6b130.tar.bz2 lua-9756f56354af72d62b4f63c85000d4ae1db6b130.zip | |
better control over accesses to TValue fields
| -rw-r--r-- | lgc.c | 8 | ||||
| -rw-r--r-- | lobject.h | 59 | ||||
| -rw-r--r-- | ltable.c | 4 | ||||
| -rw-r--r-- | ltests.c | 6 |
4 files changed, 40 insertions, 37 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 2.57 2009/09/28 16:32:50 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 2.58 2009/10/23 19:12:19 roberto Exp roberto $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -96,7 +96,7 @@ void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { | |||
| 96 | global_State *g = G(L); | 96 | global_State *g = G(L); |
| 97 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); | 97 | lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); |
| 98 | lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); | 98 | lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); |
| 99 | lua_assert(ttype(gch(o)) != LUA_TTABLE); | 99 | lua_assert(gch(o)->tt != LUA_TTABLE); |
| 100 | /* must keep invariant? */ | 100 | /* must keep invariant? */ |
| 101 | if (g->gcstate == GCSpropagate) | 101 | if (g->gcstate == GCSpropagate) |
| 102 | reallymarkobject(g, v); /* restore invariant */ | 102 | reallymarkobject(g, v); /* restore invariant */ |
| @@ -544,7 +544,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { | |||
| 544 | int deadmask = otherwhite(g); | 544 | int deadmask = otherwhite(g); |
| 545 | while ((curr = *p) != NULL && count-- > 0) { | 545 | while ((curr = *p) != NULL && count-- > 0) { |
| 546 | int alive = (gch(curr)->marked ^ WHITEBITS) & deadmask; | 546 | int alive = (gch(curr)->marked ^ WHITEBITS) & deadmask; |
| 547 | if (ttisthread(gch(curr))) | 547 | if (gch(curr)->tt == LUA_TTHREAD) |
| 548 | sweepthread(L, gco2th(curr), alive); | 548 | sweepthread(L, gco2th(curr), alive); |
| 549 | if (alive) { | 549 | if (alive) { |
| 550 | lua_assert(!isdead(g, curr) || testbit(gch(curr)->marked, FIXEDBIT)); | 550 | lua_assert(!isdead(g, curr) || testbit(gch(curr)->marked, FIXEDBIT)); |
| @@ -645,7 +645,7 @@ size_t luaC_separateudata (lua_State *L, int all) { | |||
| 645 | /* find last 'next' field in 'tobefnz' list (to insert elements in its end) */ | 645 | /* find last 'next' field in 'tobefnz' list (to insert elements in its end) */ |
| 646 | while (*lastnext != NULL) lastnext = &gch(*lastnext)->next; | 646 | while (*lastnext != NULL) lastnext = &gch(*lastnext)->next; |
| 647 | while ((curr = *p) != NULL) { /* traverse all finalizable objects */ | 647 | while ((curr = *p) != NULL) { /* traverse all finalizable objects */ |
| 648 | lua_assert(ttisuserdata(gch(curr)) && !isfinalized(gco2u(curr))); | 648 | lua_assert(gch(curr)->tt == LUA_TUSERDATA && !isfinalized(gco2u(curr))); |
| 649 | lua_assert(testbit(gch(curr)->marked, SEPARATED)); | 649 | lua_assert(testbit(gch(curr)->marked, SEPARATED)); |
| 650 | if (!(all || iswhite(curr))) /* not being collected? */ | 650 | if (!(all || iswhite(curr))) /* not being collected? */ |
| 651 | p = &gch(curr)->next; /* don't bother with it */ | 651 | p = &gch(curr)->next; /* don't bother with it */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 2.29 2009/09/28 16:32:50 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.30 2009/09/30 15:38:37 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -52,7 +52,6 @@ typedef struct GCheader { | |||
| 52 | 52 | ||
| 53 | 53 | ||
| 54 | 54 | ||
| 55 | |||
| 56 | /* | 55 | /* |
| 57 | ** Union of all Lua values | 56 | ** Union of all Lua values |
| 58 | */ | 57 | */ |
| @@ -64,11 +63,12 @@ typedef union { | |||
| 64 | } Value; | 63 | } Value; |
| 65 | 64 | ||
| 66 | 65 | ||
| 66 | |||
| 67 | /* | 67 | /* |
| 68 | ** Tagged Values | 68 | ** Tagged Values |
| 69 | */ | 69 | */ |
| 70 | 70 | ||
| 71 | #define TValuefields Value value; int tt | 71 | #define TValuefields Value value_; int tt_ |
| 72 | 72 | ||
| 73 | typedef struct lua_TValue { | 73 | typedef struct lua_TValue { |
| 74 | TValuefields; | 74 | TValuefields; |
| @@ -87,18 +87,18 @@ typedef struct lua_TValue { | |||
| 87 | #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) | 87 | #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) |
| 88 | 88 | ||
| 89 | /* Macros to access values */ | 89 | /* Macros to access values */ |
| 90 | #define ttype(o) ((o)->tt) | 90 | #define ttype(o) ((o)->tt_) |
| 91 | #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) | 91 | #define gcvalue(o) check_exp(iscollectable(o), (o)->value_.gc) |
| 92 | #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) | 92 | #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value_.p) |
| 93 | #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) | 93 | #define nvalue(o) check_exp(ttisnumber(o), (o)->value_.n) |
| 94 | #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) | 94 | #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value_.gc->ts) |
| 95 | #define tsvalue(o) (&rawtsvalue(o)->tsv) | 95 | #define tsvalue(o) (&rawtsvalue(o)->tsv) |
| 96 | #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) | 96 | #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u) |
| 97 | #define uvalue(o) (&rawuvalue(o)->uv) | 97 | #define uvalue(o) (&rawuvalue(o)->uv) |
| 98 | #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) | 98 | #define clvalue(o) check_exp(ttisfunction(o), &(o)->value_.gc->cl) |
| 99 | #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) | 99 | #define hvalue(o) check_exp(ttistable(o), &(o)->value_.gc->h) |
| 100 | #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) | 100 | #define bvalue(o) check_exp(ttisboolean(o), (o)->value_.b) |
| 101 | #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) | 101 | #define thvalue(o) check_exp(ttisthread(o), &(o)->value_.gc->th) |
| 102 | 102 | ||
| 103 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) | 103 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) |
| 104 | 104 | ||
| @@ -106,56 +106,56 @@ typedef struct lua_TValue { | |||
| 106 | ** for internal debug only | 106 | ** for internal debug only |
| 107 | */ | 107 | */ |
| 108 | #define checkconsistency(obj) \ | 108 | #define checkconsistency(obj) \ |
| 109 | lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) | 109 | lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value_.gc->gch.tt)) |
| 110 | 110 | ||
| 111 | #define checkliveness(g,obj) \ | 111 | #define checkliveness(g,obj) \ |
| 112 | lua_assert(!iscollectable(obj) || \ | 112 | lua_assert(!iscollectable(obj) || \ |
| 113 | ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) | 113 | ((ttype(obj) == (obj)->value_.gc->gch.tt) && !isdead(g, (obj)->value_.gc))) |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | /* Macros to set values */ | 116 | /* Macros to set values */ |
| 117 | #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) | 117 | #define setnilvalue(obj) ((obj)->tt_=LUA_TNIL) |
| 118 | 118 | ||
| 119 | #define setnvalue(obj,x) \ | 119 | #define setnvalue(obj,x) \ |
| 120 | { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } | 120 | { TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; } |
| 121 | 121 | ||
| 122 | #define changenvalue(obj,x) \ | 122 | #define changenvalue(obj,x) \ |
| 123 | ( lua_assert((obj)->tt==LUA_TNUMBER), (obj)->value.n=(x) ) | 123 | ( lua_assert((obj)->tt_==LUA_TNUMBER), (obj)->value_.n=(x) ) |
| 124 | 124 | ||
| 125 | #define setpvalue(obj,x) \ | 125 | #define setpvalue(obj,x) \ |
| 126 | { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } | 126 | { TValue *i_o=(obj); i_o->value_.p=(x); i_o->tt_=LUA_TLIGHTUSERDATA; } |
| 127 | 127 | ||
| 128 | #define setbvalue(obj,x) \ | 128 | #define setbvalue(obj,x) \ |
| 129 | { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } | 129 | { TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; } |
| 130 | 130 | ||
| 131 | #define setsvalue(L,obj,x) \ | 131 | #define setsvalue(L,obj,x) \ |
| 132 | { TValue *i_o=(obj); \ | 132 | { TValue *i_o=(obj); \ |
| 133 | i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ | 133 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \ |
| 134 | checkliveness(G(L),i_o); } | 134 | checkliveness(G(L),i_o); } |
| 135 | 135 | ||
| 136 | #define setuvalue(L,obj,x) \ | 136 | #define setuvalue(L,obj,x) \ |
| 137 | { TValue *i_o=(obj); \ | 137 | { TValue *i_o=(obj); \ |
| 138 | i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ | 138 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TUSERDATA; \ |
| 139 | checkliveness(G(L),i_o); } | 139 | checkliveness(G(L),i_o); } |
| 140 | 140 | ||
| 141 | #define setthvalue(L,obj,x) \ | 141 | #define setthvalue(L,obj,x) \ |
| 142 | { TValue *i_o=(obj); \ | 142 | { TValue *i_o=(obj); \ |
| 143 | i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ | 143 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTHREAD; \ |
| 144 | checkliveness(G(L),i_o); } | 144 | checkliveness(G(L),i_o); } |
| 145 | 145 | ||
| 146 | #define setclvalue(L,obj,x) \ | 146 | #define setclvalue(L,obj,x) \ |
| 147 | { TValue *i_o=(obj); \ | 147 | { TValue *i_o=(obj); \ |
| 148 | i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ | 148 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TFUNCTION; \ |
| 149 | checkliveness(G(L),i_o); } | 149 | checkliveness(G(L),i_o); } |
| 150 | 150 | ||
| 151 | #define sethvalue(L,obj,x) \ | 151 | #define sethvalue(L,obj,x) \ |
| 152 | { TValue *i_o=(obj); \ | 152 | { TValue *i_o=(obj); \ |
| 153 | i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ | 153 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTABLE; \ |
| 154 | checkliveness(G(L),i_o); } | 154 | checkliveness(G(L),i_o); } |
| 155 | 155 | ||
| 156 | #define setptvalue(L,obj,x) \ | 156 | #define setptvalue(L,obj,x) \ |
| 157 | { TValue *i_o=(obj); \ | 157 | { TValue *i_o=(obj); \ |
| 158 | i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ | 158 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TPROTO; \ |
| 159 | checkliveness(G(L),i_o); } | 159 | checkliveness(G(L),i_o); } |
| 160 | 160 | ||
| 161 | 161 | ||
| @@ -163,7 +163,7 @@ typedef struct lua_TValue { | |||
| 163 | 163 | ||
| 164 | #define setobj(L,obj1,obj2) \ | 164 | #define setobj(L,obj1,obj2) \ |
| 165 | { const TValue *o2=(obj2); TValue *o1=(obj1); \ | 165 | { const TValue *o2=(obj2); TValue *o1=(obj1); \ |
| 166 | o1->value = o2->value; o1->tt=o2->tt; \ | 166 | o1->value_ = o2->value_; o1->tt_=o2->tt_; \ |
| 167 | checkliveness(G(L),o1); } | 167 | checkliveness(G(L),o1); } |
| 168 | 168 | ||
| 169 | 169 | ||
| @@ -186,7 +186,7 @@ typedef struct lua_TValue { | |||
| 186 | #define setobj2n setobj | 186 | #define setobj2n setobj |
| 187 | #define setsvalue2n setsvalue | 187 | #define setsvalue2n setsvalue |
| 188 | 188 | ||
| 189 | #define setttype(obj, tt) (ttype(obj) = (tt)) | 189 | #define setttype(obj, tt_) (ttype(obj) = (tt_)) |
| 190 | 190 | ||
| 191 | 191 | ||
| 192 | #define iscollectable(o) (ttype(o) >= LUA_TSTRING) | 192 | #define iscollectable(o) (ttype(o) >= LUA_TSTRING) |
| @@ -341,6 +341,9 @@ typedef struct Node { | |||
| 341 | TKey i_key; | 341 | TKey i_key; |
| 342 | } Node; | 342 | } Node; |
| 343 | 343 | ||
| 344 | #define setnodekey(nd,obj) { Node *n = (nd); const TValue *o = (obj); \ | ||
| 345 | n->i_key.nk.value_ = o->value_; n->i_key.nk.tt_ = o->tt_; } | ||
| 346 | |||
| 344 | 347 | ||
| 345 | typedef struct Table { | 348 | typedef struct Table { |
| 346 | CommonHeader; | 349 | CommonHeader; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 2.41 2009/08/07 17:53:28 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.42 2009/10/23 12:31:12 roberto Exp roberto $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -418,7 +418,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | |||
| 418 | mp = n; | 418 | mp = n; |
| 419 | } | 419 | } |
| 420 | } | 420 | } |
| 421 | gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; | 421 | setnodekey(mp, key); |
| 422 | luaC_barriert(L, t, key); | 422 | luaC_barriert(L, t, key); |
| 423 | lua_assert(ttisnil(gval(mp))); | 423 | lua_assert(ttisnil(gval(mp))); |
| 424 | return gval(mp); | 424 | return gval(mp); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 2.76 2009/10/11 20:02:19 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.77 2009/10/23 19:12:19 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -203,7 +203,7 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) { | |||
| 203 | #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t))) | 203 | #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t))) |
| 204 | 204 | ||
| 205 | #define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \ | 205 | #define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \ |
| 206 | ((ttype(t) == gch((t)->value.gc)->tt) && testobjref(g,f,gcvalue(t)))) | 206 | ((ttype(t) == gch(gcvalue(t))->tt) && testobjref(g,f,gcvalue(t)))) |
| 207 | 207 | ||
| 208 | 208 | ||
| 209 | 209 | ||
| @@ -513,7 +513,7 @@ static int mem_query (lua_State *L) { | |||
| 513 | 513 | ||
| 514 | 514 | ||
| 515 | static int settrick (lua_State *L) { | 515 | static int settrick (lua_State *L) { |
| 516 | l_Trick = obj_at(L, 1)->value.gc; | 516 | l_Trick = gcvalue(obj_at(L, 1)); |
| 517 | return 0; | 517 | return 0; |
| 518 | } | 518 | } |
| 519 | 519 | ||
