00001 
00002 
00003 
00004 
00005 
00006 
00007 #ifndef lobject_h
00008 #define lobject_h
00009 
00010 
00011 #include "llimits.h"
00012 #include "lua.h"
00013 
00014 
00015 
00016 #define NUM_TAGS        LUA_TTHREAD
00017 
00018 
00019 
00020 
00021 
00022 #define LUA_TPROTO      (NUM_TAGS+1)
00023 #define LUA_TUPVAL      (NUM_TAGS+2)
00024 
00025 
00026 
00027 
00028 
00029 typedef union GCObject GCObject;
00030 
00031 
00032 
00033 
00034 
00035 
00036 #define CommonHeader      GCObject *next; lu_byte tt; lu_byte marked
00037 
00038 
00039 
00040 
00041 
00042 typedef struct GCheader {
00043   CommonHeader;
00044 } GCheader;
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 typedef union {
00053 
00054   GCObject *gc;
00055   void *p;
00056   lua_Number n;
00057   int b;
00058 } Value;
00059 
00060 
00061 
00062 
00063 
00064 typedef struct lua_TObject {
00065   int tt;
00066   Value value;
00067 } TObject;
00068 
00069 
00070 
00071 #define ttisnil(o)      (ttype(o) == LUA_TNIL)
00072 #define ttisnumber(o)   (ttype(o) == LUA_TNUMBER)
00073 #define ttisstring(o)   (ttype(o) == LUA_TSTRING)
00074 #define ttistable(o)    (ttype(o) == LUA_TTABLE)
00075 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
00076 #define ttisboolean(o)  (ttype(o) == LUA_TBOOLEAN)
00077 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
00078 #define ttisthread(o)   (ttype(o) == LUA_TTHREAD)
00079 #define ttislightuserdata(o)    (ttype(o) == LUA_TLIGHTUSERDATA)
00080 
00081 
00082 #define ttype(o)        ((o)->tt)
00083 #define gcvalue(o)      check_exp(iscollectable(o), (o)->value.gc)
00084 #define pvalue(o)       check_exp(ttislightuserdata(o), (o)->value.p)
00085 #define nvalue(o)       check_exp(ttisnumber(o), (o)->value.n)
00086 #define tsvalue(o)      check_exp(ttisstring(o), &(o)->value.gc->ts)
00087 #define uvalue(o)       check_exp(ttisuserdata(o), &(o)->value.gc->u)
00088 #define clvalue(o)      check_exp(ttisfunction(o), &(o)->value.gc->cl)
00089 #define hvalue(o)       check_exp(ttistable(o), &(o)->value.gc->h)
00090 #define bvalue(o)       check_exp(ttisboolean(o), (o)->value.b)
00091 #define thvalue(o)      check_exp(ttisthread(o), &(o)->value.gc->th)
00092 
00093 #define l_isfalse(o)    (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
00094 
00095 
00096 #define setnvalue(obj,x) \
00097   { TObject *i_o=(obj); i_o->tt=LUA_TNUMBER; i_o->value.n=(x); }
00098 
00099 #define chgnvalue(obj,x) \
00100         check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
00101 
00102 #define setpvalue(obj,x) \
00103   { TObject *i_o=(obj); i_o->tt=LUA_TLIGHTUSERDATA; i_o->value.p=(x); }
00104 
00105 #define setbvalue(obj,x) \
00106   { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); }
00107 
00108 #define setsvalue(obj,x) \
00109   { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; \
00110     i_o->value.gc=cast(GCObject *, (x)); \
00111     lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); }
00112 
00113 #define setuvalue(obj,x) \
00114   { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \
00115     i_o->value.gc=cast(GCObject *, (x)); \
00116     lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); }
00117 
00118 #define setthvalue(obj,x) \
00119   { TObject *i_o=(obj); i_o->tt=LUA_TTHREAD; \
00120     i_o->value.gc=cast(GCObject *, (x)); \
00121     lua_assert(i_o->value.gc->gch.tt == LUA_TTHREAD); }
00122 
00123 #define setclvalue(obj,x) \
00124   { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \
00125     i_o->value.gc=cast(GCObject *, (x)); \
00126     lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); }
00127 
00128 #define sethvalue(obj,x) \
00129   { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \
00130     i_o->value.gc=cast(GCObject *, (x)); \
00131     lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); }
00132 
00133 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
00134 
00135 
00136 
00137 
00138 
00139 
00140 #define checkconsistency(obj) \
00141   lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
00142 
00143 
00144 #define setobj(obj1,obj2) \
00145   { const TObject *o2=(obj2); TObject *o1=(obj1); \
00146     checkconsistency(o2); \
00147     o1->tt=o2->tt; o1->value = o2->value; }
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 #define setobjs2s       setobj
00156 
00157 #define setobj2s        setobj
00158 #define setsvalue2s     setsvalue
00159 
00160 #define setobjt2t       setobj
00161 
00162 #define setobj2t        setobj
00163 
00164 #define setobj2n        setobj
00165 #define setsvalue2n     setsvalue
00166 
00167 #define setttype(obj, tt) (ttype(obj) = (tt))
00168 
00169 
00170 #define iscollectable(o)        (ttype(o) >= LUA_TSTRING)
00171 
00172 
00173 
00174 typedef TObject *StkId;  
00175 
00176 
00177 
00178 
00179 
00180 typedef union TString {
00181   L_Umaxalign dummy;  
00182   struct {
00183     CommonHeader;
00184     lu_byte reserved;
00185     lu_hash hash;
00186     size_t len;
00187   } tsv;
00188 } TString;
00189 
00190 
00191 #define getstr(ts)      cast(const char *, (ts) + 1)
00192 #define svalue(o)       getstr(tsvalue(o))
00193 
00194 
00195 
00196 typedef union Udata {
00197   L_Umaxalign dummy;  
00198   struct {
00199     CommonHeader;
00200     struct Table *metatable;
00201     size_t len;
00202   } uv;
00203 } Udata;
00204 
00205 
00206 
00207 
00208 
00209 
00210 
00211 typedef struct Proto {
00212   CommonHeader;
00213 
00214   TObject *k;  
00215 
00216   Instruction *code;
00217 
00218   struct Proto **p;  
00219 
00220   int *lineinfo;  
00221 
00222   struct LocVar *locvars;  
00223 
00224   TString **upvalues;  
00225 
00226   TString  *source;
00227   int sizeupvalues;
00228   int sizek;  
00229   int sizecode;
00230   int sizelineinfo;
00231   int sizep;  
00232   int sizelocvars;
00233   int lineDefined;
00234 
00235   GCObject *gclist;
00236   lu_byte nups;  
00237   lu_byte numparams;
00238   lu_byte is_vararg;
00239   lu_byte maxstacksize;
00240 } Proto;
00241 
00242 
00243 typedef struct LocVar {
00244 
00245   TString *varname;
00246   int startpc;  
00247   int endpc;    
00248 } LocVar;
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 typedef struct UpVal {
00257   CommonHeader;
00258 
00259   TObject *v;  
00260   TObject value;  
00261 } UpVal;
00262 
00263 
00264 
00265 
00266 
00267 
00268 #define ClosureHeader \
00269         CommonHeader; lu_byte isC; lu_byte nupvalues;  GCObject *gclist
00270 
00271 typedef struct CClosure {
00272   ClosureHeader;
00273   lua_CFunction f;
00274   TObject upvalue[1];
00275 } CClosure;
00276 
00277 
00278 typedef struct LClosure {
00279   ClosureHeader;
00280   struct Proto *p;
00281   TObject g;  
00282   UpVal *upvals[1];
00283 } LClosure;
00284 
00285 
00286 typedef union Closure {
00287   CClosure c;
00288   LClosure l;
00289 } Closure;
00290 
00291 
00292 #define iscfunction(o)  (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
00293 #define isLfunction(o)  (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
00294 
00295 
00296 
00297 
00298 
00299 
00300 typedef struct Node {
00301   TObject i_key;
00302   TObject i_val;
00303   struct Node *next;  
00304 } Node;
00305 
00306 
00307 typedef struct Table {
00308   CommonHeader;
00309   lu_byte flags;   
00310   lu_byte lsizenode;  
00311   struct Table *metatable;
00312 
00313   TObject *array;  
00314  
00315   Node *node;
00316   Node *firstfree;  
00317   GCObject *gclist;
00318   int sizearray;  
00319 } Table;
00320 
00321 
00322 
00323 
00324 
00325 
00326 #define lmod(s,size) \
00327         check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))
00328 
00329 
00330 #define twoto(x)        (1<<(x))
00331 #define sizenode(t)     (twoto((t)->lsizenode))
00332 
00333 
00334 
00335 
00336 extern const TObject luaO_nilobject;
00337 
00338 int luaO_log2 (unsigned int x)
00339         ;
00340 int luaO_int2fb (unsigned int x)
00341         ;
00342 #define fb2int(x)       (((x) & 7) << ((x) >> 3))
00343 
00344 int luaO_rawequalObj (const TObject *t1, const TObject *t2)
00345         ;
00346 int luaO_str2d (const char *s, lua_Number *result)
00347         ;
00348 
00349 
00350 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp)
00351         ;
00352 
00353 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...)
00354         ;
00355 void luaO_chunkid (char *out, const char *source, int len)
00356         ;
00357 
00358 
00359 #endif