lua/lobject.h

Go to the documentation of this file.
00001 /*
00002 ** $Id: lobject.h,v 1.2 2004/03/23 05:09:14 jbj Exp $
00003 ** Type definitions for Lua objects
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 #ifndef lobject_h
00008 #define lobject_h
00009 
00010 
00011 #include "llimits.h"
00012 #include "lua.h"
00013 
00014 
00015 /* tags for values visible from Lua */
00016 #define NUM_TAGS        LUA_TTHREAD
00017 
00018 
00019 /*
00020 ** Extra tags for non-values
00021 */
00022 #define LUA_TPROTO      (NUM_TAGS+1)
00023 #define LUA_TUPVAL      (NUM_TAGS+2)
00024 
00025 
00026 /*
00027 ** Union of all collectable objects
00028 */
00029 typedef union GCObject GCObject;
00030 
00031 
00032 /*
00033 ** Common Header for all collectable objects (in macro form, to be
00034 ** included in other objects)
00035 */
00036 #define CommonHeader    /*@dependent@*/ /*@null@*/ GCObject *next; lu_byte tt; lu_byte marked
00037 
00038 
00039 /*
00040 ** Common header in struct form
00041 */
00042 typedef struct GCheader {
00043   CommonHeader;
00044 } GCheader;
00045 
00046 
00047 
00048 
00049 /*
00050 ** Union of all Lua values
00051 */
00052 typedef union {
00053 /*@relnull@*/
00054   GCObject *gc;
00055   void *p;
00056   lua_Number n;
00057   int b;
00058 } Value;
00059 
00060 
00061 /*
00062 ** Lua values (or `tagged objects')
00063 */
00064 typedef struct lua_TObject {
00065   int tt;
00066   Value value;
00067 } TObject;
00068 
00069 
00070 /* Macros to test type */
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 /* Macros to access values */
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 /* Macros to set values */
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 ** for internal debug only
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 ** different types of sets, according to destination
00152 */
00153 
00154 /* from stack to (same) stack */
00155 #define setobjs2s       setobj
00156 /* to stack (not from same stack) */
00157 #define setobj2s        setobj
00158 #define setsvalue2s     setsvalue
00159 /* from table to same table */
00160 #define setobjt2t       setobj
00161 /* to table */
00162 #define setobj2t        setobj
00163 /* to new object */
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;  /* index to stack elements */
00175 
00176 
00177 /*
00178 ** String headers for string table
00179 */
00180 typedef union TString {
00181   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
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;  /* ensures maximum alignment for `local' udata */
00198   struct {
00199     CommonHeader;
00200     struct Table *metatable;
00201     size_t len;
00202   } uv;
00203 } Udata;
00204 
00205 
00206 
00207 
00208 /*
00209 ** Function Prototypes
00210 */
00211 typedef struct Proto {
00212   CommonHeader;
00213 /*@relnull@*/
00214   TObject *k;  /* constants used by the function */
00215 /*@relnull@*/
00216   Instruction *code;
00217 /*@relnull@*/
00218   struct Proto **p;  /* functions defined inside the function */
00219 /*@relnull@*/
00220   int *lineinfo;  /* map from opcodes to source lines */
00221 /*@relnull@*/
00222   struct LocVar *locvars;  /* information about local variables */
00223 /*@relnull@*/
00224   TString **upvalues;  /* upvalue names */
00225 /*@relnull@*/
00226   TString  *source;
00227   int sizeupvalues;
00228   int sizek;  /* size of `k' */
00229   int sizecode;
00230   int sizelineinfo;
00231   int sizep;  /* size of `p' */
00232   int sizelocvars;
00233   int lineDefined;
00234 /*@relnull@*/
00235   GCObject *gclist;
00236   lu_byte nups;  /* number of upvalues */
00237   lu_byte numparams;
00238   lu_byte is_vararg;
00239   lu_byte maxstacksize;
00240 } Proto;
00241 
00242 
00243 typedef struct LocVar {
00244 /*@relnull@*/
00245   TString *varname;
00246   int startpc;  /* first point where variable is active */
00247   int endpc;    /* first point where variable is dead */
00248 } LocVar;
00249 
00250 
00251 
00252 /*
00253 ** Upvalues
00254 */
00255 
00256 typedef struct UpVal {
00257   CommonHeader;
00258 /*@null@*/
00259   TObject *v;  /* points to stack or to its own value */
00260   TObject value;  /* the value (when closed) */
00261 } UpVal;
00262 
00263 
00264 /*
00265 ** Closures
00266 */
00267 
00268 #define ClosureHeader \
00269         CommonHeader; lu_byte isC; lu_byte nupvalues; /*@null@*/ 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;  /* global table for this closure */
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 ** Tables
00298 */
00299 
00300 typedef struct Node {
00301   TObject i_key;
00302   TObject i_val;
00303   struct Node *next;  /* for chaining */
00304 } Node;
00305 
00306 
00307 typedef struct Table {
00308   CommonHeader;
00309   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */ 
00310   lu_byte lsizenode;  /* log2 of size of `node' array */
00311   struct Table *metatable;
00312 /*@null@*/
00313   TObject *array;  /* array part */
00314 /*@owned@*/ /*@null@*/
00315   Node *node;
00316   Node *firstfree;  /* this position is free; all positions after it are full */
00317   GCObject *gclist;
00318   int sizearray;  /* size of `array' array */
00319 } Table;
00320 
00321 
00322 
00323 /*
00324 ** `module' operation for hashing (size is always a power of 2)
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 /*@unchecked@*/
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         /*@modifies *result @*/;
00348 
00349 /*@observer@*/
00350 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp)
00351         /*@modifies L @*/;
00352 /*@observer@*/
00353 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...)
00354         /*@modifies L @*/;
00355 void luaO_chunkid (char *out, const char *source, int len)
00356         /*@modifies *out @*/;
00357 
00358 
00359 #endif

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2