- Main Page
- Classes
- Files

# lobject.h

Go to the documentation of this file.

    00001 /*
    00002 ** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $
    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 "llimits.h"
    00016 #include "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
    00089 /* Macros to access values */
    00090 #define ttype(o)        ((o)->tt)
    00091 #define gcvalue(o)      check_exp(iscollectable(o), (o)->value.gc)
    00092 #define pvalue(o)       check_exp(ttislightuserdata(o), (o)->value.p)
    00093 #define nvalue(o)       check_exp(ttisnumber(o), (o)->value.n)
    00094 #define rawtsvalue(o)   check_exp(ttisstring(o), &(o)->value.gc->ts)
    00095 #define tsvalue(o)      (&rawtsvalue(o)->tsv)
    00096 #define rawuvalue(o)    check_exp(ttisuserdata(o), &(o)->value.gc->u)
    00097 #define uvalue(o)       (&rawuvalue(o)->uv)
    00098 #define clvalue(o)      check_exp(ttisfunction(o), &(o)->value.gc->cl)
    00099 #define hvalue(o)       check_exp(ttistable(o), &(o)->value.gc->h)
    00100 #define bvalue(o)       check_exp(ttisboolean(o), (o)->value.b)
    00101 #define thvalue(o)      check_exp(ttisthread(o), &(o)->value.gc->th)
    00102
    00103 #define l_isfalse(o)    (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
    00104
    00105 /*
    00106 ** for internal debug only
    00107 */
    00108 #define checkconsistency(obj) \
    00109   lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
    00110
    00111 #define checkliveness(g,obj) \
    00112   lua_assert(!iscollectable(obj) || \
    00113   ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
    00114
    00115
    00116 /* Macros to set values */
    00117 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
    00118
    00119 #define setnvalue(obj,x) \
    00120   { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
    00121
    00122 #define setpvalue(obj,x) \
    00123   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
    00124
    00125 #define setbvalue(obj,x) \
    00126   { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
    00127
    00128 #define setsvalue(L,obj,x) \
    00129   { TValue *i_o=(obj); \
    00130     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
    00131     checkliveness(G(L),i_o); }
    00132
    00133 #define setuvalue(L,obj,x) \
    00134   { TValue *i_o=(obj); \
    00135     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
    00136     checkliveness(G(L),i_o); }
    00137
    00138 #define setthvalue(L,obj,x) \
    00139   { TValue *i_o=(obj); \
    00140     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
    00141     checkliveness(G(L),i_o); }
    00142
    00143 #define setclvalue(L,obj,x) \
    00144   { TValue *i_o=(obj); \
    00145     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
    00146     checkliveness(G(L),i_o); }
    00147
    00148 #define sethvalue(L,obj,x) \
    00149   { TValue *i_o=(obj); \
    00150     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
    00151     checkliveness(G(L),i_o); }
    00152
    00153 #define setptvalue(L,obj,x) \
    00154   { TValue *i_o=(obj); \
    00155     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
    00156     checkliveness(G(L),i_o); }
    00157
    00158
    00159
    00160
    00161 #define setobj(L,obj1,obj2) \
    00162   { const TValue *o2=(obj2); TValue *o1=(obj1); \
    00163     o1->value = o2->value; o1->tt=o2->tt; \
    00164     checkliveness(G(L),o1); }
    00165
    00166
    00167 /*
    00168 ** different types of sets, according to destination
    00169 */
    00170
    00171 /* from stack to (same) stack */
    00172 #define setobjs2s       setobj
    00173 /* to stack (not from same stack) */
    00174 #define setobj2s        setobj
    00175 #define setsvalue2s     setsvalue
    00176 #define sethvalue2s     sethvalue
    00177 #define setptvalue2s    setptvalue
    00178 /* from table to same table */
    00179 #define setobjt2t       setobj
    00180 /* to table */
    00181 #define setobj2t        setobj
    00182 /* to new object */
    00183 #define setobj2n        setobj
    00184 #define setsvalue2n     setsvalue
    00185
    00186 #define setttype(obj, tt) (ttype(obj) = (tt))
    00187
    00188
    00189 #define iscollectable(o)        (ttype(o) >= LUA_TSTRING)
    00190
    00191
    00192
    00193 typedef TValue *StkId;  /* index to stack elements */
    00194
    00195
    00196 /*
    00197 ** String headers for string table
    00198 */
    00199 typedef union TString {
    00200   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
    00201   struct {
    00202     CommonHeader;
    00203     lu_byte reserved;
    00204     unsigned int hash;
    00205     size_t len;
    00206   } tsv;
    00207 } TString;
    00208
    00209
    00210 #define getstr(ts)      cast(const char *, (ts) + 1)
    00211 #define svalue(o)       getstr(rawtsvalue(o))
    00212
    00213
    00214
    00215 typedef union Udata {
    00216   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
    00217   struct {
    00218     CommonHeader;
    00219     struct Table *metatable;
    00220     struct Table *env;
    00221     size_t len;
    00222   } uv;
    00223 } Udata;
    00224
    00225
    00226
    00227
    00228 /*
    00229 ** Function Prototypes
    00230 */
    00231 typedef struct Proto {
    00232   CommonHeader;
    00233   TValue *k;  /* constants used by the function */
    00234   Instruction *code;
    00235   struct Proto **p;  /* functions defined inside the function */
    00236   int *lineinfo;  /* map from opcodes to source lines */
    00237   struct LocVar *locvars;  /* information about local variables */
    00238   TString **upvalues;  /* upvalue names */
    00239   TString  *source;
    00240   int sizeupvalues;
    00241   int sizek;  /* size of `k' */
    00242   int sizecode;
    00243   int sizelineinfo;
    00244   int sizep;  /* size of `p' */
    00245   int sizelocvars;
    00246   int linedefined;
    00247   int lastlinedefined;
    00248   GCObject *gclist;
    00249   lu_byte nups;  /* number of upvalues */
    00250   lu_byte numparams;
    00251   lu_byte is_vararg;
    00252   lu_byte maxstacksize;
    00253 } Proto;
    00254
    00255
    00256 /* masks for new-style vararg */
    00257 #define VARARG_HASARG           1
    00258 #define VARARG_ISVARARG         2
    00259 #define VARARG_NEEDSARG         4
    00260
    00261
    00262 typedef struct LocVar {
    00263   TString *varname;
    00264   int startpc;  /* first point where variable is active */
    00265   int endpc;    /* first point where variable is dead */
    00266 } LocVar;
    00267
    00268
    00269
    00270 /*
    00271 ** Upvalues
    00272 */
    00273
    00274 typedef struct UpVal {
    00275   CommonHeader;
    00276   TValue *v;  /* points to stack or to its own value */
    00277   union {
    00278     TValue value;  /* the value (when closed) */
    00279     struct {  /* double linked list (when open) */
    00280       struct UpVal *prev;
    00281       struct UpVal *next;
    00282     } l;
    00283   } u;
    00284 } UpVal;
    00285
    00286
    00287 /*
    00288 ** Closures
    00289 */
    00290
    00291 #define ClosureHeader \
    00292         CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
    00293         struct Table *env
    00294
    00295 typedef struct CClosure {
    00296   ClosureHeader;
    00297   lua_CFunction f;
    00298   TValue upvalue[1];
    00299 } CClosure;
    00300
    00301
    00302 typedef struct LClosure {
    00303   ClosureHeader;
    00304   struct Proto *p;
    00305   UpVal *upvals[1];
    00306 } LClosure;
    00307
    00308
    00309 typedef union Closure {
    00310   CClosure c;
    00311   LClosure l;
    00312 } Closure;
    00313
    00314
    00315 #define iscfunction(o)  (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
    00316 #define isLfunction(o)  (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
    00317
    00318
    00319 /*
    00320 ** Tables
    00321 */
    00322
    00323 typedef union TKey {
    00324   struct {
    00325     TValuefields;
    00326     struct Node *next;  /* for chaining */
    00327   } nk;
    00328   TValue tvk;
    00329 } TKey;
    00330
    00331
    00332 typedef struct Node {
    00333   TValue i_val;
    00334   TKey i_key;
    00335 } Node;
    00336
    00337
    00338 typedef struct Table {
    00339   CommonHeader;
    00340   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
    00341   lu_byte lsizenode;  /* log2 of size of `node' array */
    00342   struct Table *metatable;
    00343   TValue *array;  /* array part */
    00344   Node *node;
    00345   Node *lastfree;  /* any free position is before this position */
    00346   GCObject *gclist;
    00347   int sizearray;  /* size of `array' array */
    00348 } Table;
    00349
    00350
    00351
    00352 /*
    00353 ** `module' operation for hashing (size is always a power of 2)
    00354 */
    00355 #define lmod(s,size) \
    00356         (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
    00357
    00358
    00359 #define twoto(x)        (1<<(x))
    00360 #define sizenode(t)     (twoto((t)->lsizenode))
    00361
    00362
    00363 #define luaO_nilobject          (&luaO_nilobject_)
    00364
    00365 LUAI_DATA const TValue luaO_nilobject_;
    00366
    00367 #define ceillog2(x)     (luaO_log2((x)-1) + 1)
    00368
    00369 LUAI_FUNC int luaO_log2 (unsigned int x);
    00370 LUAI_FUNC int luaO_int2fb (unsigned int x);
    00371 LUAI_FUNC int luaO_fb2int (int x);
    00372 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
    00373 LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
    00374 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
    00375                                                        va_list argp);
    00376 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
    00377 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
    00378
    00379
    00380 #endif
    00381