Nut/OS  4.10.3
API Reference
lobject.h
Go to the documentation of this file.
00001 /*
00002 ** $Id: lobject.h 2831 2009-12-08 14:19:44Z haraldkipp $
00003 ** Type definitions for Lua objects
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #ifndef lobject_h
00009 #define lobject_h
00010 
00011 
00012 #include <stdarg.h>
00013 
00014 
00015 #include <lua/llimits.h>
00016 #include <lua/lua.h>
00017 
00018 
00019 /* tags for values visible from Lua */
00020 #define LAST_TAG        LUA_TTHREAD
00021 
00022 #define NUM_TAGS        (LAST_TAG+1)
00023 
00024 
00025 /*
00026 ** Extra tags for non-values
00027 */
00028 #define LUA_TPROTO      (LAST_TAG+1)
00029 #define LUA_TUPVAL      (LAST_TAG+2)
00030 #define LUA_TDEADKEY    (LAST_TAG+3)
00031 
00032 
00033 /*
00034 ** Union of all collectable objects
00035 */
00036 typedef union GCObject GCObject;
00037 
00038 
00039 /*
00040 ** Common Header for all collectable objects (in macro form, to be
00041 ** included in other objects)
00042 */
00043 #define CommonHeader    GCObject *next; lu_byte tt; lu_byte marked
00044 
00045 
00046 /*
00047 ** Common header in struct form
00048 */
00049 typedef struct GCheader {
00050   CommonHeader;
00051 } GCheader;
00052 
00053 
00054 
00055 
00056 /*
00057 ** Union of all Lua values
00058 */
00059 typedef union {
00060   GCObject *gc;
00061   void *p;
00062   lua_Number n;
00063   int b;
00064 } Value;
00065 
00066 
00067 /*
00068 ** Tagged Values
00069 */
00070 
00071 #define TValuefields    Value value; int tt
00072 
00073 typedef struct lua_TValue {
00074   TValuefields;
00075 } TValue;
00076 
00077 
00078 /* Macros to test type */
00079 #define ttisnil(o)      (ttype(o) == LUA_TNIL)
00080 #define ttisnumber(o)   (ttype(o) == LUA_TNUMBER)
00081 #define ttisstring(o)   (ttype(o) == LUA_TSTRING)
00082 #define ttistable(o)    (ttype(o) == LUA_TTABLE)
00083 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
00084 #define ttisboolean(o)  (ttype(o) == LUA_TBOOLEAN)
00085 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
00086 #define ttisthread(o)   (ttype(o) == LUA_TTHREAD)
00087 #define ttislightuserdata(o)    (ttype(o) == LUA_TLIGHTUSERDATA)
00088 #define ttisrotable(o) (ttype(o) == LUA_TROTABLE)
00089 #define ttislightfunction(o)  (ttype(o) == LUA_TLIGHTFUNCTION)
00090 
00091 /* Macros to access values */
00092 #define ttype(o)        ((o)->tt)
00093 #define gcvalue(o)      check_exp(iscollectable(o), (o)->value.gc)
00094 #define pvalue(o)       check_exp(ttislightuserdata(o), (o)->value.p)
00095 #define rvalue(o)       check_exp(ttisrotable(o), (o)->value.p)
00096 #define fvalue(o) check_exp(ttislightfunction(o), (o)->value.p)
00097 #define nvalue(o)       check_exp(ttisnumber(o), (o)->value.n)
00098 #define rawtsvalue(o)   check_exp(ttisstring(o), &(o)->value.gc->ts)
00099 #define tsvalue(o)      (&rawtsvalue(o)->tsv)
00100 #define rawuvalue(o)    check_exp(ttisuserdata(o), &(o)->value.gc->u)
00101 #define uvalue(o)       (&rawuvalue(o)->uv)
00102 #define clvalue(o)      check_exp(ttisfunction(o), &(o)->value.gc->cl)
00103 #define hvalue(o)       check_exp(ttistable(o), &(o)->value.gc->h)
00104 #define bvalue(o)       check_exp(ttisboolean(o), (o)->value.b)
00105 #define thvalue(o)      check_exp(ttisthread(o), &(o)->value.gc->th)
00106 
00107 #define l_isfalse(o)    (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
00108 
00109 /*
00110 ** for internal debug only
00111 */
00112 #define checkconsistency(obj) \
00113   lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
00114 
00115 #define checkliveness(g,obj) \
00116   lua_assert(!iscollectable(obj) || \
00117   ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
00118 
00119 
00120 /* Macros to set values */
00121 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
00122 
00123 #define setnvalue(obj,x) \
00124   { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
00125 
00126 #define setpvalue(obj,x) \
00127   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
00128   
00129 #define setrvalue(obj,x) \
00130   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TROTABLE; }
00131   
00132 #define setfvalue(obj,x) \
00133   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTFUNCTION; }
00134 
00135 #define setbvalue(obj,x) \
00136   { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
00137 
00138 #define setsvalue(L,obj,x) \
00139   { TValue *i_o=(obj); \
00140     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
00141     checkliveness(G(L),i_o); }
00142 
00143 #define setuvalue(L,obj,x) \
00144   { TValue *i_o=(obj); \
00145     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
00146     checkliveness(G(L),i_o); }
00147 
00148 #define setthvalue(L,obj,x) \
00149   { TValue *i_o=(obj); \
00150     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
00151     checkliveness(G(L),i_o); }
00152 
00153 #define setclvalue(L,obj,x) \
00154   { TValue *i_o=(obj); \
00155     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
00156     checkliveness(G(L),i_o); }
00157 
00158 #define sethvalue(L,obj,x) \
00159   { TValue *i_o=(obj); \
00160     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
00161     checkliveness(G(L),i_o); }
00162 
00163 #define setptvalue(L,obj,x) \
00164   { TValue *i_o=(obj); \
00165     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
00166     checkliveness(G(L),i_o); }
00167 
00168 
00169 
00170 
00171 #define setobj(L,obj1,obj2) \
00172   { const TValue *o2=(obj2); TValue *o1=(obj1); \
00173     o1->value = o2->value; o1->tt=o2->tt; \
00174     checkliveness(G(L),o1); }
00175 
00176 
00177 /*
00178 ** different types of sets, according to destination
00179 */
00180 
00181 /* from stack to (same) stack */
00182 #define setobjs2s       setobj
00183 /* to stack (not from same stack) */
00184 #define setobj2s        setobj
00185 #define setsvalue2s     setsvalue
00186 #define sethvalue2s     sethvalue
00187 #define setptvalue2s    setptvalue
00188 /* from table to same table */
00189 #define setobjt2t       setobj
00190 /* to table */
00191 #define setobj2t        setobj
00192 /* to new object */
00193 #define setobj2n        setobj
00194 #define setsvalue2n     setsvalue
00195 
00196 #define setttype(obj, tt) (ttype(obj) = (tt))
00197 
00198 
00199 #define iscollectable(o)        (ttype(o) >= LUA_TSTRING)
00200 
00201 
00202 
00203 typedef TValue *StkId;  /* index to stack elements */
00204 
00205 
00206 /*
00207 ** String headers for string table
00208 */
00209 typedef union TString {
00210   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
00211   struct {
00212     CommonHeader;
00213     lu_byte reserved;
00214     unsigned int hash;
00215     size_t len;
00216   } tsv;
00217 } TString;
00218 
00219 
00220 #define getstr(ts)      cast(const char *, (ts) + 1)
00221 #define svalue(o)       getstr(rawtsvalue(o))
00222 
00223 
00224 
00225 typedef union Udata {
00226   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
00227   struct {
00228     CommonHeader;
00229     struct Table *metatable;
00230     struct Table *env;
00231     size_t len;
00232   } uv;
00233 } Udata;
00234 
00235 
00236 
00237 
00238 /*
00239 ** Function Prototypes
00240 */
00241 typedef struct Proto {
00242   CommonHeader;
00243   TValue *k;  /* constants used by the function */
00244   Instruction *code;
00245   struct Proto **p;  /* functions defined inside the function */
00246   int *lineinfo;  /* map from opcodes to source lines */
00247   struct LocVar *locvars;  /* information about local variables */
00248   TString **upvalues;  /* upvalue names */
00249   TString  *source;
00250   int sizeupvalues;
00251   int sizek;  /* size of `k' */
00252   int sizecode;
00253   int sizelineinfo;
00254   int sizep;  /* size of `p' */
00255   int sizelocvars;
00256   int linedefined;
00257   int lastlinedefined;
00258   GCObject *gclist;
00259   lu_byte nups;  /* number of upvalues */
00260   lu_byte numparams;
00261   lu_byte is_vararg;
00262   lu_byte maxstacksize;
00263 } Proto;
00264 
00265 
00266 /* masks for new-style vararg */
00267 #define VARARG_HASARG           1
00268 #define VARARG_ISVARARG         2
00269 #define VARARG_NEEDSARG         4
00270 
00271 
00272 typedef struct LocVar {
00273   TString *varname;
00274   int startpc;  /* first point where variable is active */
00275   int endpc;    /* first point where variable is dead */
00276 } LocVar;
00277 
00278 
00279 
00280 /*
00281 ** Upvalues
00282 */
00283 
00284 typedef struct UpVal {
00285   CommonHeader;
00286   TValue *v;  /* points to stack or to its own value */
00287   union {
00288     TValue value;  /* the value (when closed) */
00289     struct {  /* double linked list (when open) */
00290       struct UpVal *prev;
00291       struct UpVal *next;
00292     } l;
00293   } u;
00294 } UpVal;
00295 
00296 
00297 /*
00298 ** Closures
00299 */
00300 
00301 #define ClosureHeader \
00302         CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
00303         struct Table *env
00304 
00305 typedef struct CClosure {
00306   ClosureHeader;
00307   lua_CFunction f;
00308   TValue upvalue[1];
00309 } CClosure;
00310 
00311 
00312 typedef struct LClosure {
00313   ClosureHeader;
00314   struct Proto *p;
00315   UpVal *upvals[1];
00316 } LClosure;
00317 
00318 
00319 typedef union Closure {
00320   CClosure c;
00321   LClosure l;
00322 } Closure;
00323 
00324 
00325 #define iscfunction(o)  ((ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)||(ttype(o)==LUA_TLIGHTFUNCTION))
00326 #define isLfunction(o)  (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
00327 
00328 
00329 /*
00330 ** Tables
00331 */
00332 
00333 typedef union TKey {
00334   struct {
00335     TValuefields;
00336     struct Node *next;  /* for chaining */
00337   } nk;
00338   TValue tvk;
00339 } TKey;
00340 
00341 
00342 typedef struct Node {
00343   TValue i_val;
00344   TKey i_key;
00345 } Node;
00346 
00347 
00348 typedef struct Table {
00349   CommonHeader;
00350   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */ 
00351   lu_byte lsizenode;  /* log2 of size of `node' array */
00352   struct Table *metatable;
00353   TValue *array;  /* array part */
00354   Node *node;
00355   Node *lastfree;  /* any free position is before this position */
00356   GCObject *gclist;
00357   int sizearray;  /* size of `array' array */
00358 } Table;
00359 
00360 
00361 
00362 /*
00363 ** `module' operation for hashing (size is always a power of 2)
00364 */
00365 #define lmod(s,size) \
00366         (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
00367 
00368 
00369 #define twoto(x)        (1<<(x))
00370 #define sizenode(t)     (twoto((t)->lsizenode))
00371 
00372 
00373 #define luaO_nilobject          (&luaO_nilobject_)
00374 
00375 LUAI_DATA const TValue luaO_nilobject_;
00376 
00377 #define ceillog2(x)     (luaO_log2((x)-1) + 1)
00378 
00379 LUAI_FUNC int luaO_log2 (unsigned int x);
00380 LUAI_FUNC int luaO_int2fb (unsigned int x);
00381 LUAI_FUNC int luaO_fb2int (int x);
00382 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
00383 LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
00384 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
00385                                                        va_list argp);
00386 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
00387 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
00388 
00389 
00390 #endif
00391