1
0
mirror of https://github.com/lua/lua.git synced 2025-01-14 05:43:00 +08:00

better control over accesses to TValue fields

This commit is contained in:
Roberto Ierusalimschy 2009-11-05 15:43:54 -02:00
parent b7d5f18d71
commit 9756f56354
4 changed files with 40 additions and 37 deletions

8
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.57 2009/09/28 16:32:50 roberto Exp roberto $ ** $Id: lgc.c,v 2.58 2009/10/23 19:12:19 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -96,7 +96,7 @@ void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
global_State *g = G(L); global_State *g = G(L);
lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
lua_assert(ttype(gch(o)) != LUA_TTABLE); lua_assert(gch(o)->tt != LUA_TTABLE);
/* must keep invariant? */ /* must keep invariant? */
if (g->gcstate == GCSpropagate) if (g->gcstate == GCSpropagate)
reallymarkobject(g, v); /* restore invariant */ reallymarkobject(g, v); /* restore invariant */
@ -544,7 +544,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
int deadmask = otherwhite(g); int deadmask = otherwhite(g);
while ((curr = *p) != NULL && count-- > 0) { while ((curr = *p) != NULL && count-- > 0) {
int alive = (gch(curr)->marked ^ WHITEBITS) & deadmask; int alive = (gch(curr)->marked ^ WHITEBITS) & deadmask;
if (ttisthread(gch(curr))) if (gch(curr)->tt == LUA_TTHREAD)
sweepthread(L, gco2th(curr), alive); sweepthread(L, gco2th(curr), alive);
if (alive) { if (alive) {
lua_assert(!isdead(g, curr) || testbit(gch(curr)->marked, FIXEDBIT)); lua_assert(!isdead(g, curr) || testbit(gch(curr)->marked, FIXEDBIT));
@ -645,7 +645,7 @@ size_t luaC_separateudata (lua_State *L, int all) {
/* find last 'next' field in 'tobefnz' list (to insert elements in its end) */ /* find last 'next' field in 'tobefnz' list (to insert elements in its end) */
while (*lastnext != NULL) lastnext = &gch(*lastnext)->next; while (*lastnext != NULL) lastnext = &gch(*lastnext)->next;
while ((curr = *p) != NULL) { /* traverse all finalizable objects */ while ((curr = *p) != NULL) { /* traverse all finalizable objects */
lua_assert(ttisuserdata(gch(curr)) && !isfinalized(gco2u(curr))); lua_assert(gch(curr)->tt == LUA_TUSERDATA && !isfinalized(gco2u(curr)));
lua_assert(testbit(gch(curr)->marked, SEPARATED)); lua_assert(testbit(gch(curr)->marked, SEPARATED));
if (!(all || iswhite(curr))) /* not being collected? */ if (!(all || iswhite(curr))) /* not being collected? */
p = &gch(curr)->next; /* don't bother with it */ p = &gch(curr)->next; /* don't bother with it */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.29 2009/09/28 16:32:50 roberto Exp roberto $ ** $Id: lobject.h,v 2.30 2009/09/30 15:38:37 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -52,7 +52,6 @@ typedef struct GCheader {
/* /*
** Union of all Lua values ** Union of all Lua values
*/ */
@ -64,11 +63,12 @@ typedef union {
} Value; } Value;
/* /*
** Tagged Values ** Tagged Values
*/ */
#define TValuefields Value value; int tt #define TValuefields Value value_; int tt_
typedef struct lua_TValue { typedef struct lua_TValue {
TValuefields; TValuefields;
@ -87,18 +87,18 @@ typedef struct lua_TValue {
#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
/* Macros to access values */ /* Macros to access values */
#define ttype(o) ((o)->tt) #define ttype(o) ((o)->tt_)
#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) #define gcvalue(o) check_exp(iscollectable(o), (o)->value_.gc)
#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value_.p)
#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) #define nvalue(o) check_exp(ttisnumber(o), (o)->value_.n)
#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value_.gc->ts)
#define tsvalue(o) (&rawtsvalue(o)->tsv) #define tsvalue(o) (&rawtsvalue(o)->tsv)
#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u)
#define uvalue(o) (&rawuvalue(o)->uv) #define uvalue(o) (&rawuvalue(o)->uv)
#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) #define clvalue(o) check_exp(ttisfunction(o), &(o)->value_.gc->cl)
#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) #define hvalue(o) check_exp(ttistable(o), &(o)->value_.gc->h)
#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) #define bvalue(o) check_exp(ttisboolean(o), (o)->value_.b)
#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) #define thvalue(o) check_exp(ttisthread(o), &(o)->value_.gc->th)
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
@ -106,56 +106,56 @@ typedef struct lua_TValue {
** for internal debug only ** for internal debug only
*/ */
#define checkconsistency(obj) \ #define checkconsistency(obj) \
lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value_.gc->gch.tt))
#define checkliveness(g,obj) \ #define checkliveness(g,obj) \
lua_assert(!iscollectable(obj) || \ lua_assert(!iscollectable(obj) || \
((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) ((ttype(obj) == (obj)->value_.gc->gch.tt) && !isdead(g, (obj)->value_.gc)))
/* Macros to set values */ /* Macros to set values */
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) #define setnilvalue(obj) ((obj)->tt_=LUA_TNIL)
#define setnvalue(obj,x) \ #define setnvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } { TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; }
#define changenvalue(obj,x) \ #define changenvalue(obj,x) \
( lua_assert((obj)->tt==LUA_TNUMBER), (obj)->value.n=(x) ) ( lua_assert((obj)->tt_==LUA_TNUMBER), (obj)->value_.n=(x) )
#define setpvalue(obj,x) \ #define setpvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } { TValue *i_o=(obj); i_o->value_.p=(x); i_o->tt_=LUA_TLIGHTUSERDATA; }
#define setbvalue(obj,x) \ #define setbvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } { TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; }
#define setsvalue(L,obj,x) \ #define setsvalue(L,obj,x) \
{ TValue *i_o=(obj); \ { TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \
checkliveness(G(L),i_o); } checkliveness(G(L),i_o); }
#define setuvalue(L,obj,x) \ #define setuvalue(L,obj,x) \
{ TValue *i_o=(obj); \ { TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TUSERDATA; \
checkliveness(G(L),i_o); } checkliveness(G(L),i_o); }
#define setthvalue(L,obj,x) \ #define setthvalue(L,obj,x) \
{ TValue *i_o=(obj); \ { TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTHREAD; \
checkliveness(G(L),i_o); } checkliveness(G(L),i_o); }
#define setclvalue(L,obj,x) \ #define setclvalue(L,obj,x) \
{ TValue *i_o=(obj); \ { TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TFUNCTION; \
checkliveness(G(L),i_o); } checkliveness(G(L),i_o); }
#define sethvalue(L,obj,x) \ #define sethvalue(L,obj,x) \
{ TValue *i_o=(obj); \ { TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTABLE; \
checkliveness(G(L),i_o); } checkliveness(G(L),i_o); }
#define setptvalue(L,obj,x) \ #define setptvalue(L,obj,x) \
{ TValue *i_o=(obj); \ { TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TPROTO; \
checkliveness(G(L),i_o); } checkliveness(G(L),i_o); }
@ -163,7 +163,7 @@ typedef struct lua_TValue {
#define setobj(L,obj1,obj2) \ #define setobj(L,obj1,obj2) \
{ const TValue *o2=(obj2); TValue *o1=(obj1); \ { const TValue *o2=(obj2); TValue *o1=(obj1); \
o1->value = o2->value; o1->tt=o2->tt; \ o1->value_ = o2->value_; o1->tt_=o2->tt_; \
checkliveness(G(L),o1); } checkliveness(G(L),o1); }
@ -186,7 +186,7 @@ typedef struct lua_TValue {
#define setobj2n setobj #define setobj2n setobj
#define setsvalue2n setsvalue #define setsvalue2n setsvalue
#define setttype(obj, tt) (ttype(obj) = (tt)) #define setttype(obj, tt_) (ttype(obj) = (tt_))
#define iscollectable(o) (ttype(o) >= LUA_TSTRING) #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
@ -341,6 +341,9 @@ typedef struct Node {
TKey i_key; TKey i_key;
} Node; } Node;
#define setnodekey(nd,obj) { Node *n = (nd); const TValue *o = (obj); \
n->i_key.nk.value_ = o->value_; n->i_key.nk.tt_ = o->tt_; }
typedef struct Table { typedef struct Table {
CommonHeader; CommonHeader;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.41 2009/08/07 17:53:28 roberto Exp roberto $ ** $Id: ltable.c,v 2.42 2009/10/23 12:31:12 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -418,7 +418,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
mp = n; mp = n;
} }
} }
gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; setnodekey(mp, key);
luaC_barriert(L, t, key); luaC_barriert(L, t, key);
lua_assert(ttisnil(gval(mp))); lua_assert(ttisnil(gval(mp)));
return gval(mp); return gval(mp);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.76 2009/10/11 20:02:19 roberto Exp roberto $ ** $Id: ltests.c,v 2.77 2009/10/23 19:12:19 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -203,7 +203,7 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) {
#define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t))) #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
#define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \ #define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \
((ttype(t) == gch((t)->value.gc)->tt) && testobjref(g,f,gcvalue(t)))) ((ttype(t) == gch(gcvalue(t))->tt) && testobjref(g,f,gcvalue(t))))
@ -513,7 +513,7 @@ static int mem_query (lua_State *L) {
static int settrick (lua_State *L) { static int settrick (lua_State *L) {
l_Trick = obj_at(L, 1)->value.gc; l_Trick = gcvalue(obj_at(L, 1));
return 0; return 0;
} }