From 5a9cc57a5ef75a28cd887715175a5a18db58616e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Jul 2014 14:09:50 -0300 Subject: change in GCObject: instead of being a union, it is now a structure with the common header of all collectable objects; union is used only for conversions. (Goal is to be able to check that the cast 'obj2gco' can have a check to ensure that object being converted is really a collectable object.). This is the first step in the change. --- lobject.h | 16 +++++++++------- lstate.h | 33 +++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lobject.h b/lobject.h index 72820220..d55a347f 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.93 2014/05/29 19:30:07 roberto Exp roberto $ +** $Id: lobject.h,v 2.94 2014/06/19 18:39:36 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -69,9 +69,9 @@ /* -** Union of all collectable objects +** Common type for all collectable objects */ -typedef union GCObject GCObject; +typedef struct GCObject GCObject; /* @@ -82,11 +82,13 @@ typedef union GCObject GCObject; /* -** Common header in struct form +** Common type has only the common header */ -typedef struct GCheader { - CommonHeader; -} GCheader; +struct GCObject { + struct { + CommonHeader; + } gch; +}; diff --git a/lstate.h b/lstate.h index 8b7031f8..e7feb099 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.107 2014/06/12 19:07:30 roberto Exp roberto $ +** $Id: lstate.h,v 2.108 2014/07/17 13:53:37 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -172,8 +172,8 @@ struct lua_State { /* ** Union of all collectable objects */ -union GCObject { - GCheader gch; /* common header */ +union GCUnion { + GCObject gc; /* common header */ union TString ts; union Udata u; union Closure cl; @@ -185,22 +185,31 @@ union GCObject { #define gch(o) (&(o)->gch) +#define cast_u(o) cast(union GCUnion *, (o)) + /* macros to convert a GCObject into a specific value */ #define rawgco2ts(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) + check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((cast_u(o))->ts)) #define gco2ts(o) (&rawgco2ts(o)->tsv) -#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) +#define rawgco2u(o) \ + check_exp((o)->gch.tt == LUA_TUSERDATA, &((cast_u(o))->u)) #define gco2u(o) (&rawgco2u(o)->uv) -#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) -#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) +#define gco2lcl(o) \ + check_exp((o)->gch.tt == LUA_TLCL, &((cast_u(o))->cl.l)) +#define gco2ccl(o) \ + check_exp((o)->gch.tt == LUA_TCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ - check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) -#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) -#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) -#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) + check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) +#define gco2t(o) \ + check_exp((o)->gch.tt == LUA_TTABLE, &((cast_u(o))->h)) +#define gco2p(o) \ + check_exp((o)->gch.tt == LUA_TPROTO, &((cast_u(o))->p)) +#define gco2th(o) \ + check_exp((o)->gch.tt == LUA_TTHREAD, &((cast_u(o))->th)) + /* macro to convert any Lua object into a GCObject */ -#define obj2gco(v) (cast(GCObject *, (v))) +#define obj2gco(v) (&(cast_u(v)->gc)) /* actual number of total bytes allocated */ -- cgit v1.2.3-55-g6feb