lua/lobject.h

Go to the documentation of this file.
00001 /*
00002 ** $Id: lobject.h,v 2.64 2011/10/31 17:48:22 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 /*
00020 ** Extra tags for non-values
00021 */
00022 #define LUA_TPROTO  LUA_NUMTAGS
00023 #define LUA_TUPVAL  (LUA_NUMTAGS+1)
00024 #define LUA_TDEADKEY    (LUA_NUMTAGS+2)
00025 
00026 /*
00027 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
00028 */
00029 #define LUA_TOTALTAGS   (LUA_TUPVAL+2)
00030 
00031 
00032 /*
00033 ** tags for Tagged Values have the following use of bits:
00034 ** bits 0-3: actual tag (a LUA_T* value)
00035 ** bits 4-5: variant bits
00036 ** bit 6: whether value is collectable
00037 */
00038 
00039 /*
00040 ** LUA_TFUNCTION variants:
00041 ** 0 - Lua function
00042 ** 1 - light C function
00043 ** 2 - regular C function (closure)
00044 */
00045 
00046 /* Variant tags for functions */
00047 #define LUA_TLCL    (LUA_TFUNCTION | (0 << 4))  /* Lua closure */
00048 #define LUA_TLCF    (LUA_TFUNCTION | (1 << 4))  /* light C function */
00049 #define LUA_TCCL    (LUA_TFUNCTION | (2 << 4))  /* C closure */
00050 
00051 
00052 /* Bit mark for collectable types */
00053 #define BIT_ISCOLLECTABLE   (1 << 6)
00054 
00055 /* mark a tag as collectable */
00056 #define ctb(t)          ((t) | BIT_ISCOLLECTABLE)
00057 
00058 
00059 /*
00060 ** Union of all collectable objects
00061 */
00062 typedef union GCObject GCObject;
00063 
00064 
00065 /*
00066 ** Common Header for all collectable objects (in macro form, to be
00067 ** included in other objects)
00068 */
00069 #define CommonHeader    GCObject *next; lu_byte tt; lu_byte marked
00070 
00071 
00072 /*
00073 ** Common header in struct form
00074 */
00075 typedef struct GCheader {
00076   CommonHeader;
00077 } GCheader;
00078 
00079 
00080 
00081 /*
00082 ** Union of all Lua values
00083 */
00084 typedef union Value Value;
00085 
00086 
00087 #define numfield    lua_Number n;    /* numbers */
00088 
00089 
00090 
00091 /*
00092 ** Tagged Values. This is the basic representation of values in Lua,
00093 ** an actual value plus a tag with its type.
00094 */
00095 
00096 #define TValuefields    Value value_; int tt_
00097 
00098 typedef struct lua_TValue TValue;
00099 
00100 
00101 /* macro defining a nil value */
00102 #define NILCONSTANT {NULL}, LUA_TNIL
00103 
00104 
00105 #define val_(o)     ((o)->value_)
00106 #define num_(o)     (val_(o).n)
00107 
00108 
00109 /* raw type tag of a TValue */
00110 #define rttype(o)   ((o)->tt_)
00111 
00112 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
00113 #define ttype(o)    (rttype(o) & 0x3F)
00114 
00115 
00116 /* type tag of a TValue with no variants (bits 0-3) */
00117 #define ttypenv(o)  (rttype(o) & 0x0F)
00118 
00119 
00120 /* Macros to test type */
00121 #define checktag(o,t)       (rttype(o) == (t))
00122 #define ttisnumber(o)       checktag((o), LUA_TNUMBER)
00123 #define ttisnil(o)      checktag((o), LUA_TNIL)
00124 #define ttisboolean(o)      checktag((o), LUA_TBOOLEAN)
00125 #define ttislightuserdata(o)    checktag((o), LUA_TLIGHTUSERDATA)
00126 #define ttisstring(o)       checktag((o), ctb(LUA_TSTRING))
00127 #define ttistable(o)        checktag((o), ctb(LUA_TTABLE))
00128 #define ttisfunction(o)     (ttypenv(o) == LUA_TFUNCTION)
00129 #define ttisclosure(o)      ((rttype(o) & 0x1F) == LUA_TFUNCTION)
00130 #define ttisCclosure(o)     checktag((o), ctb(LUA_TCCL))
00131 #define ttisLclosure(o)     checktag((o), ctb(LUA_TLCL))
00132 #define ttislcf(o)      checktag((o), LUA_TLCF)
00133 #define ttisuserdata(o)     checktag((o), ctb(LUA_TUSERDATA))
00134 #define ttisthread(o)       checktag((o), ctb(LUA_TTHREAD))
00135 #define ttisdeadkey(o)      checktag((o), LUA_TDEADKEY)
00136 
00137 #define ttisequal(o1,o2)    (rttype(o1) == rttype(o2))
00138 
00139 /* Macros to access values */
00140 #define nvalue(o)   check_exp(ttisnumber(o), num_(o))
00141 #define gcvalue(o)  check_exp(iscollectable(o), val_(o).gc)
00142 #define pvalue(o)   check_exp(ttislightuserdata(o), val_(o).p)
00143 #define rawtsvalue(o)   check_exp(ttisstring(o), &val_(o).gc->ts)
00144 #define tsvalue(o)  (&rawtsvalue(o)->tsv)
00145 #define rawuvalue(o)    check_exp(ttisuserdata(o), &val_(o).gc->u)
00146 #define uvalue(o)   (&rawuvalue(o)->uv)
00147 #define clvalue(o)  check_exp(ttisclosure(o), &val_(o).gc->cl)
00148 #define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
00149 #define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
00150 #define fvalue(o)   check_exp(ttislcf(o), val_(o).f)
00151 #define hvalue(o)   check_exp(ttistable(o), &val_(o).gc->h)
00152 #define bvalue(o)   check_exp(ttisboolean(o), val_(o).b)
00153 #define thvalue(o)  check_exp(ttisthread(o), &val_(o).gc->th)
00154 /* a dead value may get the 'gc' field, but cannot access its contents */
00155 #define deadvalue(o)    check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
00156 
00157 #define l_isfalse(o)    (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
00158 
00159 
00160 #define iscollectable(o)    (rttype(o) & BIT_ISCOLLECTABLE)
00161 
00162 
00163 /* Macros for internal tests */
00164 #define righttt(obj)        (ttypenv(obj) == gcvalue(obj)->gch.tt)
00165 
00166 #define checkliveness(g,obj) \
00167     lua_longassert(!iscollectable(obj) || \
00168             (righttt(obj) && !isdead(g,gcvalue(obj))))
00169 
00170 
00171 /* Macros to set values */
00172 #define settt_(o,t) ((o)->tt_=(t))
00173 
00174 #define setnvalue(obj,x) \
00175   { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
00176 
00177 #define changenvalue(o,x)   check_exp(ttisnumber(o), num_(o)=(x))
00178 
00179 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
00180 
00181 #define setfvalue(obj,x) \
00182   { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
00183 
00184 #define setpvalue(obj,x) \
00185   { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
00186 
00187 #define setbvalue(obj,x) \
00188   { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
00189 
00190 #define setgcovalue(L,obj,x) \
00191   { TValue *io=(obj); GCObject *i_g=(x); \
00192     val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
00193 
00194 #define setsvalue(L,obj,x) \
00195   { TValue *io=(obj); \
00196     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \
00197     checkliveness(G(L),io); }
00198 
00199 #define setuvalue(L,obj,x) \
00200   { TValue *io=(obj); \
00201     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
00202     checkliveness(G(L),io); }
00203 
00204 #define setthvalue(L,obj,x) \
00205   { TValue *io=(obj); \
00206     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
00207     checkliveness(G(L),io); }
00208 
00209 #define setclLvalue(L,obj,x) \
00210   { TValue *io=(obj); \
00211     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
00212     checkliveness(G(L),io); }
00213 
00214 #define setclCvalue(L,obj,x) \
00215   { TValue *io=(obj); \
00216     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
00217     checkliveness(G(L),io); }
00218 
00219 #define sethvalue(L,obj,x) \
00220   { TValue *io=(obj); \
00221     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
00222     checkliveness(G(L),io); }
00223 
00224 #define setptvalue(L,obj,x) \
00225   { TValue *io=(obj); \
00226     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \
00227     checkliveness(G(L),io); }
00228 
00229 #define setdeadvalue(obj)   settt_(obj, LUA_TDEADKEY)
00230 
00231 
00232 
00233 #define setobj(L,obj1,obj2) \
00234     { const TValue *io2=(obj2); TValue *io1=(obj1); \
00235       io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
00236       checkliveness(G(L),io1); }
00237 
00238 
00239 /*
00240 ** different types of assignments, according to destination
00241 */
00242 
00243 /* from stack to (same) stack */
00244 #define setobjs2s   setobj
00245 /* to stack (not from same stack) */
00246 #define setobj2s    setobj
00247 #define setsvalue2s setsvalue
00248 #define sethvalue2s sethvalue
00249 #define setptvalue2s    setptvalue
00250 /* from table to same table */
00251 #define setobjt2t   setobj
00252 /* to table */
00253 #define setobj2t    setobj
00254 /* to new object */
00255 #define setobj2n    setobj
00256 #define setsvalue2n setsvalue
00257 
00258 
00259 
00260 
00261 /*
00262 ** {======================================================
00263 ** NaN Trick
00264 ** =======================================================
00265 */
00266 
00267 #if defined(LUA_NANTRICK) \
00268  || defined(LUA_NANTRICK_LE) \
00269  || defined(LUA_NANTRICK_BE)
00270 
00271 /*
00272 ** numbers are represented in the 'd_' field. All other values have the
00273 ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
00274 ** a "signaled NaN", which is never generated by regular operations by
00275 ** the CPU (nor by 'strtod')
00276 */
00277 #if !defined(NNMARK)
00278 #define NNMARK      0x7FF7A500
00279 #define NNMASK      0x7FFFFF00
00280 #endif
00281 
00282 #undef TValuefields
00283 #undef NILCONSTANT
00284 
00285 #if defined(LUA_NANTRICK_LE)
00286 
00287 /* little endian */
00288 #define TValuefields  \
00289     union { struct { Value v__; int tt__; } i; double d__; } u
00290 #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
00291 /* field-access macros */
00292 #define v_(o)       ((o)->u.i.v__)
00293 #define d_(o)       ((o)->u.d__)
00294 #define tt_(o)      ((o)->u.i.tt__)
00295 
00296 #elif defined(LUA_NANTRICK_BE)
00297 
00298 /* big endian */
00299 #define TValuefields  \
00300     union { struct { int tt__; Value v__; } i; double d__; } u
00301 #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
00302 /* field-access macros */
00303 #define v_(o)       ((o)->u.i.v__)
00304 #define d_(o)       ((o)->u.d__)
00305 #define tt_(o)      ((o)->u.i.tt__)
00306 
00307 #elif !defined(TValuefields)
00308 #error option 'LUA_NANTRICK' needs declaration for 'TValuefields'
00309 
00310 #endif
00311 
00312 
00313 /* correspondence with standard representation */
00314 #undef val_
00315 #define val_(o)     v_(o)
00316 #undef num_
00317 #define num_(o)     d_(o)
00318 
00319 
00320 #undef numfield
00321 #define numfield    /* no such field; numbers are the entire struct */
00322 
00323 /* basic check to distinguish numbers from non-numbers */
00324 #undef ttisnumber
00325 #define ttisnumber(o)   ((tt_(o) & NNMASK) != NNMARK)
00326 
00327 #define tag2tt(t)   (NNMARK | (t))
00328 
00329 #undef rttype
00330 #define rttype(o)   (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
00331 
00332 #undef settt_
00333 #define settt_(o,t) (tt_(o) = tag2tt(t))
00334 
00335 #undef setnvalue
00336 #define setnvalue(obj,x) \
00337     { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
00338 
00339 #undef setobj
00340 #define setobj(L,obj1,obj2) \
00341     { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
00342       o1_->u = o2_->u; \
00343       checkliveness(G(L),o1_); }
00344 
00345 
00346 /*
00347 ** these redefinitions are not mandatory, but these forms are more efficient
00348 */
00349 
00350 #undef checktag
00351 #define checktag(o,t)   (tt_(o) == tag2tt(t))
00352 
00353 #undef ttisequal
00354 #define ttisequal(o1,o2)  \
00355     (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
00356 
00357 
00358 
00359 #define luai_checknum(L,o,c)    { if (!ttisnumber(o)) c; }
00360 
00361 
00362 #else
00363 
00364 #define luai_checknum(L,o,c)    { /* empty */ }
00365 
00366 #endif
00367 /* }====================================================== */
00368 
00369 
00370 
00371 /*
00372 ** {======================================================
00373 ** types and prototypes
00374 ** =======================================================
00375 */
00376 
00377 
00378 union Value {
00379   GCObject *gc;    /* collectable objects */
00380   void *p;         /* light userdata */
00381   int b;           /* booleans */
00382   lua_CFunction f; /* light C functions */
00383   numfield         /* numbers */
00384 };
00385 
00386 
00387 struct lua_TValue {
00388   TValuefields;
00389 };
00390 
00391 
00392 typedef TValue *StkId;  /* index to stack elements */
00393 
00394 
00395 
00396 
00397 /*
00398 ** Header for string value; string bytes follow the end of this structure
00399 */
00400 typedef union TString {
00401   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
00402   struct {
00403     CommonHeader;
00404     lu_byte reserved;
00405     unsigned int hash;
00406     size_t len;  /* number of characters in string */
00407   } tsv;
00408 } TString;
00409 
00410 
00411 /* get the actual string (array of bytes) from a TString */
00412 #define getstr(ts)  cast(const char *, (ts) + 1)
00413 
00414 /* get the actual string (array of bytes) from a Lua value */
00415 #define svalue(o)       getstr(rawtsvalue(o))
00416 
00417 
00418 /*
00419 ** Header for userdata; memory area follows the end of this structure
00420 */
00421 typedef union Udata {
00422   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
00423   struct {
00424     CommonHeader;
00425     struct Table *metatable;
00426     struct Table *env;
00427     size_t len;  /* number of bytes */
00428   } uv;
00429 } Udata;
00430 
00431 
00432 
00433 /*
00434 ** Description of an upvalue for function prototypes
00435 */
00436 typedef struct Upvaldesc {
00437   TString *name;  /* upvalue name (for debug information) */
00438   lu_byte instack;  /* whether it is in stack */
00439   lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
00440 } Upvaldesc;
00441 
00442 
00443 /*
00444 ** Description of a local variable for function prototypes
00445 ** (used for debug information)
00446 */
00447 typedef struct LocVar {
00448   TString *varname;
00449   int startpc;  /* first point where variable is active */
00450   int endpc;    /* first point where variable is dead */
00451 } LocVar;
00452 
00453 
00454 /*
00455 ** Function Prototypes
00456 */
00457 typedef struct Proto {
00458   CommonHeader;
00459   TValue *k;  /* constants used by the function */
00460   Instruction *code;
00461   struct Proto **p;  /* functions defined inside the function */
00462   int *lineinfo;  /* map from opcodes to source lines (debug information) */
00463   LocVar *locvars;  /* information about local variables (debug information) */
00464   Upvaldesc *upvalues;  /* upvalue information */
00465   union Closure *cache;  /* last created closure with this prototype */
00466   TString  *source;  /* used for debug information */
00467   int sizeupvalues;  /* size of 'upvalues' */
00468   int sizek;  /* size of `k' */
00469   int sizecode;
00470   int sizelineinfo;
00471   int sizep;  /* size of `p' */
00472   int sizelocvars;
00473   int linedefined;
00474   int lastlinedefined;
00475   GCObject *gclist;
00476   lu_byte numparams;  /* number of fixed parameters */
00477   lu_byte is_vararg;
00478   lu_byte maxstacksize;  /* maximum stack used by this function */
00479 } Proto;
00480 
00481 
00482 
00483 /*
00484 ** Lua Upvalues
00485 */
00486 typedef struct UpVal {
00487   CommonHeader;
00488   TValue *v;  /* points to stack or to its own value */
00489   union {
00490     TValue value;  /* the value (when closed) */
00491     struct {  /* double linked list (when open) */
00492       struct UpVal *prev;
00493       struct UpVal *next;
00494     } l;
00495   } u;
00496 } UpVal;
00497 
00498 
00499 /*
00500 ** Closures
00501 */
00502 
00503 #define ClosureHeader \
00504     CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist
00505 
00506 typedef struct CClosure {
00507   ClosureHeader;
00508   lua_CFunction f;
00509   TValue upvalue[1];  /* list of upvalues */
00510 } CClosure;
00511 
00512 
00513 typedef struct LClosure {
00514   ClosureHeader;
00515   struct Proto *p;
00516   UpVal *upvals[1];  /* list of upvalues */
00517 } LClosure;
00518 
00519 
00520 typedef union Closure {
00521   CClosure c;
00522   LClosure l;
00523 } Closure;
00524 
00525 
00526 #define isLfunction(o)  ttisLclosure(o)
00527 
00528 #define getproto(o) (clLvalue(o)->p)
00529 
00530 
00531 /*
00532 ** Tables
00533 */
00534 
00535 typedef union TKey {
00536   struct {
00537     TValuefields;
00538     struct Node *next;  /* for chaining */
00539   } nk;
00540   TValue tvk;
00541 } TKey;
00542 
00543 
00544 typedef struct Node {
00545   TValue i_val;
00546   TKey i_key;
00547 } Node;
00548 
00549 
00550 typedef struct Table {
00551   CommonHeader;
00552   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
00553   lu_byte lsizenode;  /* log2 of size of `node' array */
00554   struct Table *metatable;
00555   TValue *array;  /* array part */
00556   Node *node;
00557   Node *lastfree;  /* any free position is before this position */
00558   GCObject *gclist;
00559   int sizearray;  /* size of `array' array */
00560 } Table;
00561 
00562 
00563 
00564 /*
00565 ** `module' operation for hashing (size is always a power of 2)
00566 */
00567 #define lmod(s,size) \
00568     (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
00569 
00570 
00571 #define twoto(x)    (1<<(x))
00572 #define sizenode(t) (twoto((t)->lsizenode))
00573 
00574 
00575 /*
00576 ** (address of) a fixed nil value
00577 */
00578 #define luaO_nilobject      (&luaO_nilobject_)
00579 
00580 
00581 LUAI_DDEC const TValue luaO_nilobject_;
00582 
00583 
00584 LUAI_FUNC int luaO_int2fb (unsigned int x);
00585 LUAI_FUNC int luaO_fb2int (int x);
00586 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
00587 LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
00588 LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
00589 LUAI_FUNC int luaO_hexavalue (int c);
00590 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
00591                                                        va_list argp);
00592 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
00593 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
00594 
00595 
00596 #endif
00597 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:04 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs